Eclipse Community Forums - RDF feed
https://www.eclipse.org/forums/
Eclipse Community Forums@Onetomany problems
https://www.eclipse.org/forums/index.php/mv/msg/367554/895831/#msg_895831
Using Eclipse 2.3.0
Relationship One Mainform has many Students added to it (using JSF).
In order to make my relationship to work I ended up to the following setup which I do not understand the reason for the problems I had before finding this setup.
Problems I faced until I found the above correct (??) way.
1. In my JSF form I would add a new Student in the table (java list).
When I called em.merge(mainform) it would update the mainform and also create the new student in the related students table. When the user pressed Save in JSF againg, that is when the em.merge(mainform) was called again, without any other changes, the same Student was re-inserted in the table. By making the FetchType set to LAZY as shown above the problem was solved (I really don't understand why).
The main form entity was updated and the related Student was not re-inserted.
2. The next problem I faced was that when I entered a new StudentB in my students list and then em.merge(mainform), correctly it updated the database by adding the new studentB in the related table.
Then when i modified StudentA from the students list, on merge, it would update correctly StudentA in the database BUT it would recreate once again the StudentB.
The solution that I came up with was the addition of orphanRemoval =true as shown above.
Again I do not understand the reason why this behaviour.
Could someone explain me why it behaves like this.
Thank you very much.]]>Maria Michael2012-07-16T08:50:11-00:00Re: @Onetomany problems
https://www.eclipse.org/forums/index.php/mv/msg/367554/895947/#msg_895947
It is "working" when you use lazy and orphan removal because the collection is not read in when you find MainForm. With change detection, the list does not need to be retreived when adding a new Student. So subsequent merges only see the untriggered collection and so skip it. When you trigger the collection and modify a student from the list, on a subsequent merge JPA will see that there are X objects in the collection and merge them. It will find a student without an ID in it that is different from the collection it has in memory - Orphan removal will cause the new Student added in the first merge to be removed and this new student to be persisted in its place. Turning Eclipselink logging to the Finest level should show you what is happening.
if this is the case, you can get around adding orphan removal (unless it fits in with what is needed from the app) by returning and using the copy returned from em.Merge() and by calling flush after the merge so that all entities have their pks set. If this is not the case, please describe a bit more about the entities and what you see in the logs.
Best Regards,
Chris]]>Chris Delahunt2012-07-16T15:37:09-00:00