开发者

Java: Objects relations with criteria (hibernate)

开发者 https://www.devze.com 2023-01-24 04:39 出处:网络
I have a problem with criteria and relation between objects. I present a simple scheme before: I have 2 objects:

I have a problem with criteria and relation between objects. I present a simple scheme before:

I have 2 objects:

A is the parent B is the child with a link at parent.

I know how to create a query to have B with A's restriction:

Criteria criteria = this.getSession().createCriteria(B.class);
criteria.add(Property.forName("a.name").eq("test"));

But my problem is the next:

How to retrieve all A objects with a B's restricition without direct link between A at B ?

Thanks f开发者_运维问答or your help.


If you don't have a direct link between A and B in your object graph, the only way to do what you want will be to create a custom SQL query (or HQL) in your criteria.

Similar to

criteria.add ( Restrictions.sqlRestriction("b.parent_id = a.id"))

Check the documentation on criteria for more information.


just check the posts here it's useful and I tried it and it worked https://forum.hibernate.org/viewtopic.php?t=943792

0

精彩评论

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

关注公众号