是否可以使用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')
您可以使用此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));