休眠标准API'选择' [英] Hibernate criteria api 'Select in'
本文介绍了休眠标准API'选择'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能通过hibernate critiria api创建'select in'查询?
示例:
我有两张表,分别是1: n关系,公司和部门
select * from公司c其中c.id in(从部门选择company_id d
其中d.departmentname ='HR'和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标准= DetachedCriteria.forClass(Company.class);
criteria.add(Property.forName(id)。in(subCriteria));
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')
解决方案
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));
这篇关于休眠标准API'选择'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文