views:

822

answers:

1

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 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.

A: 

This should work:

.add(Subqueries.geAll(value, detachedCriteria))

CodeAddict
HI,thx. I checked this but my idea is to define a DetachedCriteria that would be reusable. So the 2nd criteria should be able to use its own restrictions + the restrictions defined in the DetachedCriteria.
dawez