Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » @Onetomany problems
@Onetomany problems [message #895831] Mon, 16 July 2012 08:50 Go to next message
Maria Michael is currently offline Maria MichaelFriend
Messages: 1
Registered: July 2012
Junior Member
Hello, this is the first time I post to this forum so I apologize for any mistakes i make.

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.

Relationship on the One side

@OneToMany(mappedBy = "mainform", fetch = FetchType.LAZY, cascade={CascadeType.ALL},orphanRemoval=true)
private List<Student> students;

Relationship on the Many Side

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "MAINFORM_ID", referencedColumnName = "MAINFORM_ID")
private MainForm mainform;



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.
Re: @Onetomany problems [message #895947 is a reply to message #895831] Mon, 16 July 2012 15:37 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
There are a few things that could be going wrong. Are you using sequencing on Students? Chances are that the entities you are using and passing around are frequently detached, and the new Students you pass around do not have a primary key set. When you call merge, the EntityManager returns the managed MainForm copy of what you passed in - your application should use this copy instead of the one you passed in. When you call merge again, the copy of MainForm is referencing students that don't have pks set, and so they must be treated as new again.

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
Previous Topic:Multitenant Shared Data
Next Topic:Dynamic entities: adding properties after initialization
Goto Forum:
  


Current Time: Thu Apr 25 19:21:44 GMT 2024

Powered by FUDForum. Page generated in 0.03074 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top