小编典典

休眠条件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')

阅读 253

收藏
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