Hibernate Subquery and DetachedCriteria
- by dawez
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.