Hibernate Subquery and DetachedCriteria
Posted
by dawez
on Stack Overflow
See other posts from Stack Overflow
or by dawez
Published on 2010-05-07T09:04:25Z
Indexed on
2010/05/07
9:08 UTC
Read the original article
Hit count: 373
I have created a DetachedCriteria that is retrieving estates that have the isApproved and isPublished set to true. It is defined in this way:
DetachedCriteria activePublishedCriteria = DetachedCriteria.forClass(Estate.class)
.add(Restrictions.eq("isApproved", true))
.add(Restrictions.eq("isPublished", true))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
I would like to reuse this criteria in some of the queries. In this case I would like to replace the isApproved and isPublished restrictions with the DetachedCriteria
Criteria criteria = getSession().createCriteria(Estate.class)
.createAlias("city", "c")
.add(Restrictions.eq("c.id", cityID))
// the following 2 lines should be use the DetachedCriteria
.add(Restrictions.eq("isApproved", true))
.add(Restrictions.eq("isPublished", true))
.setProjection(Projections.rowCount());
return (Integer) criteria.list().get(0);
Is there a way to do this ? Tried to use
.add.Subqueries.geAll(....
But cannot make it work properly. I could not find proper documentation on the Subqueries in Hibernate. Tips are welcomed.
© Stack Overflow or respective owner