如何在具有多个子查询的休眠中编写子查询。例如
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='abc@email.com'))
我知道我们可以通过 DetachedCriteria 编写,但找不到任何可以使用多个子查询的示例。
如何在具有多个子查询的休眠中编写子查询。例如
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='abc@email.com'))
我知道我们可以通过 DetachedCriteria 编写,但找不到任何可以使用多个子查询的示例。
这是一个例子:
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() 这样的布尔运算符:
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)));
完全使用分离标准来完成(因为我喜欢在没有会话的情况下构建分离标准)
DetachedCriteria idQuery = DetachedCriteria.forClass(MyPersistedObject.class)
.setProjection(Property.forName("id"))
DetachedCriteria recordQuery = DetachedCriteria.forClass(MyPersistedObject.class)
.add(Property.forName("id").eq(idQuery) );