Thanks to this post, a clear explaination about how to specify a Many-to-many relation between 2 entities with a <master> and <slaves>.

When you map a same "ManyToMany relationShip" in both directions, you??
need to tell which of these two directions is (kind of) the "master".??
So : one of your @ManyToMany annotations must have the info :??
"mappedBy" :??

For example : if you have two entities : "Event" and "User", and a??
User can "follow" several "Events",??
and an Event can be followed by several Users ("ManyToMany??
relationShip").??

You would have :??

in the entity User :??
@ManyToMany(cascade=CascadeType.ALL)??
?? ?? public Set<Event> followedEvents = new HashSet<Event>();??

in the entity Event :??
@ManyToMany(mappedBy="followedEvents")??
?? ?? public Set<User> followsByUsers = new HashSet<User>();??

Thanks to Dam74 !
Publicités