Hibernate标准api'选择'

时间:2010-11-04 13:34:13

标签: hibernate subquery hibernate-criteria

是否可以使用hibernate critiria api创建“select in”查询?

示例: 我在1:n关系,公司和部门中有两个表

select * from company c where c.id in (select company_id from department d 
where d.departmentname = 'HR' and d.location = 'xyz')

1 个答案:

答案 0 :(得分:13)

您可以使用此DetachedCriteria

DetachedCriteria subCriteria= DetachedCriteria.forClass(Departament.class);
     subCriteria.add(Property.forName("departmentname ").eq("HR"));
     subCriteria.add(Property.forName("location ").eq("xyz"));
     subCriteria.setProjection(Projections.property("company_id "));

DetachedCriteria criteria = DetachedCriteria.forClass(Company.class);
     criteria.add(Property.forName("id").in(subCriteria));