开发者

question on Hibernate Criteria API

开发者 https://www.devze.com 2023-03-30 15:35 出处:网络
I have a two table A and B: A columns (ID,NameA,BiD) B columns (ID,NameB) ID is referenced to Bid as foreign RelationMapping.

I have a two table A and B:

A columns (ID,NameA,BiD)
B columns (ID,NameB)  

ID is referenced to Bid as foreign RelationMapping. Here is the problem .My Hibenate B Entity has not got List but My hibernate A entity has got B entity 开发者_StackOverflow社区instance.

select a.* from A a, B b 
where a.BiD=b.ID

How can ı do this query in Hibernate Criteria Api? I hope I can explain my problem?


You don't need to join the classes in the query. This information is in the mapping file. If you load A's, the B's are loaded (probably lazily) too.

Criteria c = session.createCriteria(A.class).list();

If this answer doesn't help, you forgot to provide some information about the query you need.


Assuming that what you want is to find all A instances having a specific B ID

Criteria c = session.createCriteria(A.class, "a");
c.add(Restrictions.eq("a.b.id", theIdOfB);
return c.list();

If you had to search on B's name rather than B's ID, you would need a join:

Criteria c = session.createCriteria(A.class, "a");
c.createAlias("a.b", "b");
c.add(Restrictions.eq("b.name", theNameOfB);
return c.list();

As always, all this is explained with examples in the reference documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号