I’m wondering if there is any JPQL construct that might help me with this problem:
Imagine simple M-N relationship of Chat and User (user might be present in multiple chat-s).
public class ChatEntity implements Serializable {
@ManyToMany(targetEntity = UserEntity.class)
@JoinTable(name = "CHATS_USERS",
joinColumns = @JoinColumn(name = "CHAT_ID"),
inverseJoinColumns = @JoinColumn(name = "USER_ID"))
private Collection<User> users;
}
Lets imagine these data (C1..CN = chat IDs, U1..UN = user IDs)
c1 ~ {u1, u2, u3} // in chat1 is user1, user2 and user3
c2 ~ {u1, u3}
c3 ~ {u2, u3}
Now I want to select such chat which has exact same collection of users i will pass to the query. E.g.
{u1, u3} -> c2
{u1, u2, u3} -> c1
{u1, u2} -> NULL (no result)
I found MEMBER OF constuct which seems nice. E.g.
SELECT c FROM Chat c WHERE (:u1 MEMBER OF c.users) AND (:u2 MEMBER OF c.users)
... AND (:uN MEMBER OF c.users)
But now there are two problems
-
This is not correct so far because {u1, u3} will return both c1 and c2 and not only c2 as expected
-
I got the collection of users and I don’t know if there is a way how to “decompose” such collection to multiple AND clauses in named query (static definition of query) or if there is any JPQL construct which will help me with this. E.g. something like
… WHERE :users = c.users
So… you want the users to only include those Users?
Perhaps also include
Or maybe,