Reputation: 4544
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.
Upvotes: 2
Views: 191
Reputation: 4544
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.
Upvotes: 0
Reputation: 4122
You could try Set.removeAll()
or Set.retainAll()
with your two sets.
Upvotes: 1