Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Deletion of OneToMany unidirectional mapping does not delete all children
Deletion of OneToMany unidirectional mapping does not delete all children [message #1258467] Thu, 27 February 2014 12:17 Go to next message
kon f is currently offline kon fFriend
Messages: 112
Registered: March 2012
Senior Member
Hey,

I'm trying to delete some children of an entity. Here is my unidirectional OneToMany mapping:

@Entity
public class Student {

    //...

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinTable(name="STUDENT_FK", referencedColumnName="ID")
    private List<Course> courses;

    //...
}


Assume, that courses has a size of 5 and if I perform the following code, the size remains with 4. Only one element is randomly deleted.

// Student is detached!
student.getCourses().clear();
entityManager.merge(student);
entityManager.flush();


Am I doing something wrong? Or is this a bug? If I delete only one element from courses it works as expected.

Thanks.

Kon

[Updated on: Tue, 11 March 2014 10:23]

Report message to a moderator

Re: Deletion of OneToMany unidirectional mapping does not delete all children [message #1263475 is a reply to message #1258467] Tue, 04 March 2014 14:13 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
How are you getting the Student, and does the managed student instance have all the courses associated to it before you merge in the empty list?
Re: Deletion of OneToMany unidirectional mapping does not delete all children [message #1264458 is a reply to message #1263475] Wed, 05 March 2014 08:36 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 112
Registered: March 2012
Senior Member
Hey Chris,

thank you for the reply. The student instance is detached then is comes from the network (but was originally loaded from the database). To visualize my use case, I mentioned the steps I perform.

1. load the student with the corresponding courses from the database.
Students:
         ID | Name
         ==============================
          1   Steve

Courses: 
         ID | STUDENT_FK | Name
         ==============================
          1   1            Math
          2   1            History
          3   1            Computer Science 

2. ship it over the network to the fat client
3. the client might change the course in the following (removes the Math and History course):
Students:
         ID | Name
         ==============================
          1   Steve

Courses: 
         ID | STUDENT_FK | Name
         ==============================
          3   1            Computer Science 

4. if the user hits save, the student is shipped back over the network to the server
5. try to merge the detached student instance with the database
entityManager.merge(student);
entityManager.flush();



I thought if all ids (the student id as well as all course ids) are set correctly the merge should work out. So is there any issue with my approach? I haven't had this scenario before ... Smile Thank you.

Konstantin

[Updated on: Tue, 11 March 2014 10:23]

Report message to a moderator

Re: Deletion of OneToMany unidirectional mapping does not delete all children [message #1268398 is a reply to message #1264458] Mon, 10 March 2014 13:02 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 112
Registered: March 2012
Senior Member
Hey,

I was wondering if this is a bug? Or am I doing something wrong? A possible workaround would be a comparison by hand. Basically loading the actual student from the database and compare the courses one by one. If one is missing on the instance that came back from the client, I would perform a remove followed by a flush.

Kon
Re: Deletion of OneToMany unidirectional mapping does not delete all children [message #1268450 is a reply to message #1268398] Mon, 10 March 2014 14:14 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
I just noticed your jointable annotation marks the join table name as "STUDENT" - this seems to match what the default for the Student entity would be. If so, then your mapping is incorrect. A jointable is used when you want to specify that foreign keys are stored in a separate reference table outside the entity's tables - much like a M:M. Either create a new table for the references, or the foreign key needs to go in the Course's table so that it can reference the STUDENT table as a 1:1.
Re: Deletion of OneToMany unidirectional mapping does not delete all children [message #1269062 is a reply to message #1268450] Tue, 11 March 2014 10:28 Go to previous message
kon f is currently offline kon fFriend
Messages: 112
Registered: March 2012
Senior Member
Hey Chris,

sorry for this confusion, I adjusted my mapping. Basically my Course table has a FK column (with the name STUDENT_FK) that contains the student ID, it belongs to. So I hope that my mapping is fine with the adjustments Smile

I use an oracle db and my IDs have the type raw.

Thank you for your help!

Kon
Previous Topic:Next release?
Next Topic:Ordered collections: ordered-column causes NPE
Goto Forum:
  


Current Time: Mon Dec 22 20:10:13 GMT 2014

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

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