Hibernate 子查询和 DetachedCriteria
我创建了一个 DetachedCriteria,用于检索 isApproved 和 isPublished 设置为 true 的资产。它是这样定义的:
DetachedCriteria activePublishedCriteria = DetachedCriteria.forClass(Estate.class)
.add(Restrictions.eq("isApproved", true))
.add(Restrictions.eq("isPublished", true))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
我想在某些查询中重用此条件。在这种情况下,我想用 DetachedCriteria 替换 isApproved 和 isPublished 限制
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);
有没有办法做到这一点?尝试使用
.add(Subqueries.geAll(....
但无法使其正常工作。我找不到有关 Hibernate 子查询的正确文档。欢迎提示。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效:
.add(Subqueries.geAll(value, detachedCriteria))
This should work:
.add(Subqueries.geAll(value, detachedCriteria))