• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cascade delete to the parent association

 
Cristian Vrabie
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! I have this class Tag that has a set of children Tag objects. A tag can be child of more than one tag. So the mapping is:However there is a problem when deleting a Tag. All children of the deleted Tag are also deleted because of the cascade option (normal behaviour), however the Tag won't be delete from it's parents sets (the tag_relation join table), so an association will remain with an inexisting object. If I also add cascade delete or all on the parents set then the parents will also be deleted when deleting the tag which is not right. The only solution now is: at delete fetch the parents and delete the object from each association before the actual delete. This seems like a stretch. Isn't there any cascading option to automatically do this?
 
Cristian Vrabie
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any thoughts?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there isn't because cascade options are about what the database does, not what your Java objects do. That is in code and in your code.

That is why they say things like if you add a reference to an object on one side, you are responsible for setting it back the other side if it is a bi-directional mapping.

It sounds like your first option is what you want in the database. For the Java side, you will need to write code that will basically loop through that objects parent tags, and get the child tags from each individual parent's child collection and remove the child that you are about to delete.

Mark
 
Cristian Vrabie
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, It kinda makes seance, if you look at a bigger picture than my particular situation. Hibernate can't tell (or it would be very expensive to find out) what objects have association with what are you deleting, and afterwards it can't know how the association is called and what to place instead of the deleted object (supposing null is not allowed).
What I do now is exactly what you suggested: before deleting I go into parent tags and delete my object from their child list.
Thanks Mark!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic