开发者

Translate HQL subqueries to Criteria

I would like to translate this structure of HQL:开发者_如何学Python

FROM Entity_1 obj
WHERE obj IN (FROM Entity2) OR 
      obj IN (FROM Entity3)

How can it be done?


It is explained in Hibernate criteria documentation: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html#querycriteria-detachedqueries

A DetachedCriteria can also be used to express a subquery. Criterion instances involving subqueries can be obtained via Subqueries or Property.

DetachedCriteria avgWeight = DetachedCriteria.forClass(Cat.class)
    .setProjection( Property.forName("weight").avg() );
session.createCriteria(Cat.class)
    .add( Property.forName("weight").gt(avgWeight) )
    .list();

DetachedCriteria weights = DetachedCriteria.forClass(Cat.class)
    .setProjection( Property.forName("weight") );
session.createCriteria(Cat.class)
    .add( Subqueries.geAll("weight", weights) )
    .list();

Correlated subqueries are also possible:

DetachedCriteria avgWeightForSex = DetachedCriteria.forClass(Cat.class, "cat2")
    .setProjection( Property.forName("weight").avg() )
    .add( Property.forName("cat2.sex").eqProperty("cat.sex") );
session.createCriteria(Cat.class, "cat")
    .add( Property.forName("weight").gt(avgWeightForSex) )
    .list();


I am pretty new to HQL but I would suggest something like the following:

 DetachedCriteria sub1 = DetachedCriteria.forClass(Entitiy2);
 DetachedCriteria sub2 = DetachedCriteria.forClass(Entity3);

 Criteria criteria = getYourSession().createCriteria(Entity_1.class, "obj");
 criteria.add(Restrictions.or(Subqueries.propertyIn("obj", sub1), Subqueries.propertyIn("obj", sub2));

Please, correct me if I am wrong.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜