aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate: How to avoid cascading delete in bi-dir ManytoMany with Jointable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: How to avoid cascading delete in bi-dir ManytoMany with Jointable?" Watch "Hibernate: How to avoid cascading delete in bi-dir ManytoMany with Jointable?" New topic
Author

Hibernate: How to avoid cascading delete in bi-dir ManytoMany with Jointable?

yves dorji
Greenhorn

Joined: Sep 14, 2009
Posts: 6
a student can have many courses and a course can have multiple students. THus, it is modeled as bidirectional manyTomany with a jointable, the owner being course.
But with any of the Cascading enumeration type i tried (persist, merge, refresh..) or even none specified, the delete of a student deletes automatically all the courses the student was associated with. Which I absolutely wnat to avoid.
How is that achievable with annotations?
Help greatly appreciated,
thanks

Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Please show your mappings/classes.
yves dorji
Greenhorn

Joined: Sep 14, 2009
Posts: 6
I dont have the code with me right now, i ll post it in a few hours. But any general principles regarding that issue you might think of?
thanks
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Deleting a student should only delete the associations inside the jointable but not the course. (I don't even know if that's possible.)
yves dorji
Greenhorn

Joined: Sep 14, 2009
Posts: 6
even if cascade = CascadeType.ALL would be specified or somehow defaulted?
thanks
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Tried it myself... You are right. Deleting objects of the "owning" side, when deleting from the inverse side produces problems.

I found this thread: http://www.coderanch.com/t/217600/Object-Relational-Mapping/java/ManyToMany-delete-inverse-side-object
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
That's the workaround:



You retrieve all the courses the students is in and remove the link to that course.
yves dorji
Greenhorn

Joined: Sep 14, 2009
Posts: 6
Many thanks for your looking at that.
If i understand correctly, accroding to that solution i would need to override remove() of class Student and add that code snippet before calling super.remove().
I understand that, that way, Course (the owner) after removal of the student from the course, would update immediatly the join table, and the subesequent super.remove() would act on an association where the student is gone. That makes sense, i ll try that tonight.
Im not sure i understand the need for Criteria though..

yves dorji
Greenhorn

Joined: Sep 14, 2009
Posts: 6
just wondering if you tested that solution. If not I m wondering if it would not induce a circular dependency problem. By removing the student manually from the student sets in a given course, would it not fail because of the reverse dependency (the course being in the set of course of that student).
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Of course it's tested. Did you ever release code without testing?!? ;-)

Course:


Student:


Test:


Statements produced:


First Hibernate deletes the two links and then the student.

I ignored the set of courses inside that student because it's getting ignored by Hibernate as we are on the inverse side.
yves dorji
Greenhorn

Joined: Sep 14, 2009
Posts: 6
Great stuff!
testing before release..? never heard of that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate: How to avoid cascading delete in bi-dir ManytoMany with Jointable?
 
Similar Threads
EJB Entity: ManyToMany and Delete
many to many
unwanted cascade delete behaviour
Bad cascade
delete child rows using hibernate. (cascade?)