我想翻译这个HQL结构:
FROM Entity_1 obj
WHERE obj IN (FROM Entity2) OR
obj IN (FROM Entity3)
怎么做?
答案 0 :(得分:1)
Hibernate标准文档中解释了这一点:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html#querycriteria-detachedqueries
DetachedCriteria也可用于表示子查询。涉及子查询的标准实例可以通过子查询或属性获得。
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();
相关的子查询也是可能的:
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();
答案 1 :(得分:1)
我对HQL很新,但我建议如下:
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));
如果我错了,请纠正我。