根据实体列表查找实体

发布于 2024-10-15 12:01:48 字数 787 浏览 10 评论 0原文

假设我们有 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 技术交流群。

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

发布评论

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

评论(2

守望孤独 2024-10-22 12:01:48

您可以尝试使用 Set.removeAll()Set.retainAll() 处理您的两个集合。

You could try Set.removeAll() or Set.retainAll() with your two sets.

热风软妹 2024-10-22 12:01:48

正如我在前面的评论中所说,使用哈希是我能找到的唯一方法,让我相信不可能通过集合进行查询。

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.

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