Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
yves dorji
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show your mappings/classes.
 
yves dorji
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even if cascade = CascadeType.ALL would be specified or somehow defaulted?
thanks
 
Christian Dillinger
Ranch Hand
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the workaround:



You retrieve all the courses the students is in and remove the link to that course.
 
yves dorji
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great stuff!
testing before release..? never heard of that
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic