一尘不染

休眠条件API“选择范围”

hibernate

是否可以使用hibernatecritiria api创建“选择输入”查询?

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

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

阅读 203

收藏
2020-06-20

共1个答案

一尘不染

您可以使用此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));
2020-06-20