Hibernate:Criteria API:查询 CollectionOfElements 中不包含指定元素的实体?

发布于 2024-11-09 05:32:23 字数 708 浏览 4 评论 0原文

假设我有以下两门课程;用户和位置。我想创建一个 DetachedCriteria 来查询用户表,并返回没有名称为“xyz”的位置的所有用户。

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    @CollectionOfElements
    Set<Location> locations;
}

@Entity
public class Location{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    @Column
    String name;
}

以下代码将返回设置了名称“xyz”位置的所有用户:

DetachedCriteria dc = DetachedCriteria.forClass(User.class);
dc.createCriteria("locations")
     dc.add(Restrictions.eq("name", "xyz"));

如果我更改为 Restrictions.ne(),则不起作用,因为它只会返回实际设置了位置的用户。此外,如果为某个用户设置了一堆位置,它会一遍又一遍地重复该用户。

有什么想法吗?

Let's say I have the following two classes; User and Location. I want to create a DetachedCriteria to query the user table, and return all users who do not have a location with the name "xyz".

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    @CollectionOfElements
    Set<Location> locations;
}

@Entity
public class Location{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    @Column
    String name;
}

The following code will return all users who DO have a location with name "xyz" set:

DetachedCriteria dc = DetachedCriteria.forClass(User.class);
dc.createCriteria("locations")
     dc.add(Restrictions.eq("name", "xyz"));

If I change to Restrictions.ne(), that doesn't work, because it will only return users who actually have locations set. Also, if there are a bunch of locations set for a user, it will duplicate that user over and over.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

£冰雨忧蓝° 2024-11-16 05:32:23

使用原始帖子中所述的实体用户位置

1) 检查关联的位置是否为空。

Junction or = Restrictions.disjunction();
or.add(Restrictions.isEmpty("locations"));

2) 使用 LEFT_JOIN 和要在“ne”限制中使用的别名为位置创建关联条件。 LEFT_JOIN 是必需的,因此即使位置关系为空,我们仍然可以获得用户记录。

userCriteria.createCriteria("locations", "loc", Criteria.LEFT_JOIN);
or.add(Restrictions.ne("loc.name", "xyz"));

3) 将析取添加到原始用户条件。

userCriteria.add(or);

Using the entities User and Location as described in your original post:

1) Check if the associated locations is empty.

Junction or = Restrictions.disjunction();
or.add(Restrictions.isEmpty("locations"));

2) Create an associated criteria for locations using a LEFT_JOIN and an alias to be used in the "ne" restriction. The LEFT_JOIN is required so we still get User records back even if the locations relationship is empty.

userCriteria.createCriteria("locations", "loc", Criteria.LEFT_JOIN);
or.add(Restrictions.ne("loc.name", "xyz"));

3) Add the disjunction to the the original User criteria.

userCriteria.add(or);
送你一个梦 2024-11-16 05:32:23

您可能需要使用 Restrictions.and()Restrictions.not()Restrictions.or() 和 < code>Restrictions.in() 让你的逆逻辑正常工作。检查 http://docs.jboss.org/ hibernate/core/3.5/api/org/hibernate/criterion/Restrictions.html - 绝对可以做到!

为了确保您最多只获得一个User,请使用Distinct投影(http://docs.jboss.org/hibernate/core/3.5/api/org /hibernate/criterion/Distinct.html) 通过 Projections.distinct() 工厂方法(http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/criterion/Projections.html)-即:

dc.setProjection(Projections.distinct(Projections.property("id")));

类似的东西应该可以满足您的需要。

You'll probably need to use some combination of Restrictions.and(), Restrictions.not(), Restrictions.or() and Restrictions.in() to get your inverse-logic to work right. Check http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/criterion/Restrictions.html - it can definitely be done!

To ensure you only get at-most-one User, use a Distinct projection (http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/criterion/Distinct.html) via the Projections.distinct() factory method (http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/criterion/Projections.html)- i.e.:

dc.setProjection(Projections.distinct(Projections.property("id")));

Something like that should do what you need.

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