Hibernate:Criteria API:查询 CollectionOfElements 中不包含指定元素的实体?
假设我有以下两门课程;用户和位置。我想创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用原始帖子中所述的实体用户和位置:
1) 检查关联的位置是否为空。
2) 使用 LEFT_JOIN 和要在“ne”限制中使用的别名为位置创建关联条件。 LEFT_JOIN 是必需的,因此即使位置关系为空,我们仍然可以获得用户记录。
3) 将析取添加到原始用户条件。
Using the entities User and Location as described in your original post:
1) Check if the associated locations is empty.
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.
3) Add the disjunction to the the original User criteria.
您可能需要使用
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)-即:类似的东西应该可以满足您的需要。
You'll probably need to use some combination of
Restrictions.and()
,Restrictions.not()
,Restrictions.or()
andRestrictions.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 aDistinct
projection (http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/criterion/Distinct.html) via theProjections.distinct()
factory method (http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/criterion/Projections.html)- i.e.:Something like that should do what you need.