Hibernate criteria api 'Select in'

梦想的初衷 提交于 2019-12-19 06:17:23

问题


is it possible to create a 'select in'-query with the hibernate critiria api ?

Example : I have two tables in a 1:n relation, company and department

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

回答1:


You can use for this 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));


来源:https://stackoverflow.com/questions/4097161/hibernate-criteria-api-select-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!