-
1. Re: Many to Many realtionship question
jaikiran Aug 13, 2009 1:29 PM (in response to eladkatz)This wiki https://www.jboss.org/community/wiki/EJB3relationships has good informative explanations on EJB3 entity relationships. See if it covers your case (probably does).
-
2. Re: Many to Many realtionship question
eladkatz Aug 13, 2009 2:03 PM (in response to eladkatz)Hi jaikiran,
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? -
3. Re: Many to Many realtionship question
wolfgangknauf Aug 17, 2009 8:13 AM (in response to eladkatz)Hi,
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
Wolfgang