Thanks for the quick reply, I have actually read this article before and indeed in my implementation I use Sets instead of a Collection as is recommended there.
However, that article doesn't address my issue directly though it does say this:
For a @ManyToMany relationship, it might look like this:
LeftBean left = ...; RightBean right = ...; left.getRights().add(right); right.getLefts().add(left); this.entityManager.merge(left);
which means that if I want to add multiple records to the left bean i would have to add it once in the right bean and for each of the left records i'm going to have to add this right bean to them, and then only commit (merge) one of them.
that will work, but feels wrong - is this really the case?
you basically need two steps to persist data:
1) create a correct in-memory state
2) persist it
It seems that your original use case failed in step 1: the relation has to be defined correct in memory, so that both sides know each other.
If step 1 has the correct outcome, step 2 should work no matter which side is saved.
BUT you should take care for your "Cascade" type: with a "@ManyToMany", the default is to cascade nothing. So, if your relationship contains a new child item, it is not inserted by default. You have to set "cascade = CascadeType.MERGE" to insert new childs when saving the parent.
Hope this helps