-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Delete associations to parent automatically (cascade)?
PostPosted: Sat Mar 28, 2009 8:16 am 
Newbie

Joined: Wed May 21, 2008 11:21 am
Posts: 11
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:

1. <class name="Tag" table="tag">
2. <id name="id">
3. <generator class="assigned" />
4. </id>
5. <version name="version" unsaved-value="null" />
6. <property name="name" />
7.
8. <set name="children" table="tag_relation" lazy="true" fetch="select" cascade="all-delete-orphan">
9. <key column="parentId" />
10. <many-to-many column="childId" class="Tag" />
11. </set>
12.
13. <set name="parents" table="tag_relation" inverse="true" lazy="true" fetch="select">
14. <key column="childId" />
15. <many-to-many column="parentId" class="Tag" />
16. </set>
17. </class>

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?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 28, 2009 9:08 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
It is not recommended to add a cascade to a many-to-many association. Why should the childs be deleted if they can be in another tag?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 28, 2009 1:37 pm 
Newbie

Joined: Wed May 21, 2008 11:21 am
Posts: 11
I see your point mmerder. What you say would be normally true. The cascade delete is a particularity of my situation.
As for the parents association, after some thoughts I think I can answer myself: It's not possible. If you look at the bigger picture rather than my particular situation, it kinda makes seance.
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). I'll stick to my current solution: before deleting I go into parent tags and delete my object from their child list.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.