根据实体列表查找实体
假设我们有 Group ->成员(多对多)
对于给定的一组成员 - 查找是否已为这些成员创建组的最实用方法是什么? (没有额外或缺席的成员)
这感觉是错误的,因为组似乎是由它的状态定义的,但系统要求我们只能为特定的成员组合(包括顺序!)
编辑:
我正在寻找使用 JPA 来完成此操作的方法 - 所以 Set.retainAll 似乎不适用。
我想说的是,这将是完美的:
List<Member> members;
... // members gets assigned
TypedQuery<Group> query = entityManager.createQuery("select g from Group g where g.members = ?", Group.class)
query.setParameter(1, members);
Group group = query.getSingleResult();
当然不是这样的。考虑最糟糕的实现,
List<Group> groups = entityManager.createQuery("select g from Group g", Group.class).getResultList();
for (Group g: groups) {
if (g.getMembers().equals(members)) {
return g;
}
}
在这两者之间一定有我所缺少的东西。
Suppose we have Group -> Member (ManyToMany)
With a given set of Member - what's the most practical way to find if a Group has been created for exactly those members? (No additional or absent members)
This feels wrong, because Group seems to be defined by it's state, but the system requires us to only ever have one Group for a specific combination of members (including order!)
Edit:
I'm looking for a way to accomplish this using JPA - so Set.retainAll doesn't seem to apply.
What I'm saying is, this would be perfect:
List<Member> members;
... // members gets assigned
TypedQuery<Group> query = entityManager.createQuery("select g from Group g where g.members = ?", Group.class)
query.setParameter(1, members);
Group group = query.getSingleResult();
Of course that isn't how it works. Consider the worst implementation
List<Group> groups = entityManager.createQuery("select g from Group g", Group.class).getResultList();
for (Group g: groups) {
if (g.getMembers().equals(members)) {
return g;
}
}
Somewhere between those two has to be something I am missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用
Set.removeAll()
或Set.retainAll()
处理您的两个集合。You could try
Set.removeAll()
orSet.retainAll()
with your two sets.正如我在前面的评论中所说,使用哈希是我能找到的唯一方法,让我相信不可能通过集合进行查询。
Using a hash, as I stated in my comment way back, was the only approach I could find leading me to believe that it's not possible to query by a collection.