开发者

Hibernate Subquery and DetachedCriteria

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开发者_Go百科.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.


This should work:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜