I have this entity Friendship
:
@ManyToOne
private User user1;
@ManyToOne
private User user2;
... and other properties of the friendship
Per friendship there is only one instance/record of this entity (not 2), so there can't be any issues where John is a friend of Jane but not the other way around. The entries are also normalized so the member of the friendship with the lowest ID is user1
, and the other member is user2
, meaning I can prevent duplicatie entries with a simple unique constraint.
To get the friendships of a certain user I perform a query with
WHERE user1 = :me OR user2 = :me
.
Is it possible to map that onto a @OneToMany Set<Friendship>
property of User
?
(Friendship
has other properties, so this is not simply @ManyToMany Set<User> friends
)