Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate criteria api 'Select in'

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')
like image 371
ABX Avatar asked Nov 04 '10 13:11

ABX


1 Answers

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));
like image 158
Damian Leszczyński - Vash Avatar answered Sep 23 '22 21:09

Damian Leszczyński - Vash