Hibernate 子查询和 DetachedCriteria

发布于 2024-08-31 11:23:09 字数 1055 浏览 6 评论 0原文

我创建了一个 DetachedCriteria,用于检索 isApprovedisPublished 设置为 true 的资产。它是这样定义的:

DetachedCriteria activePublishedCriteria = DetachedCriteria.forClass(Estate.class)
    .add(Restrictions.eq("isApproved", true))
    .add(Restrictions.eq("isPublished", true))
    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

我想在某些查询中重用此条件。在这种情况下,我想用 DetachedCriteria 替换 isApprovedisPublished 限制

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2024-09-07 11:23:10

这应该有效:

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

This should work:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文