本文介绍了Hibernate子查询detachedCriteria的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在有多个子查询的hibernate中编写子查询。例如
How to write a subquery in hibernate which is having multiple subqueries. for example
select * from project_dtls where project_id in
(select project_id from project_users where user_id =
(select user_id from user_dtls where email='[email protected]'))
我知道我们可以写通过DetachedCriteria,但无法找到任何示例,我可以使用多个子查询。
I know that we can write through DetachedCriteria but couldnot find any example where I can use multiple subqueries.
推荐答案
下面是一个例子:
$ b
Here's an example:
DetachedCriteria exampleSubquery = DetachedCriteria.forClass(MyPersistedObject.class)
.setProjection(Property.forName("id"))
// plus any other criteria...
;
Criteria criteria = getSession().createCriteria(ARelatedPersistedObject.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.add(Subqueries.propertyIn("myPersistedObjectId", exampleSubquery)));
对于多个子查询,您可以使用一个布尔运算符,如Restrictions.or():
For multiple subqueries, you can use a boolean operator like Restrictions.or():
DetachedCriteria anotherSubquery = DetachedCriteria.forClass(MyPersistedObject.class)
.setProjection(Property.forName("id"))
// plus any other criteria...
;
Criteria criteria = getSession().createCriteria(ARelatedPersistedObject.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.add(Restrictions.or(
Subqueries.propertyIn("myPersistedObjectId", exampleSubquery),
Subqueries.propertyIn("myPersistedObjectId", anotherSubquery)));
这篇关于Hibernate子查询detachedCriteria的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!