-->
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: many-to-many delete cascade
PostPosted: Sat Jul 17, 2004 5:25 pm 
Newbie

Joined: Sat Jul 17, 2004 5:13 pm
Posts: 8
Location: Edegem, Belgium
Hello,

I have some problem that probably has a simple solution, but I am not able to come up with it.

I implemented the "Category" and "Item" class, but with a "twist".
1) a Category is owned by 1 parentCategory and can have zero or more childCategories.
2) an Item is owned by 1 Category; a Category has an Item set of the Items it owns.
3) a Category also has a itemRefs list: this is a list of Items to which the Category has a "reference". And an Item has a list of Categories, that have a reference to the Item.
4) an Item has a categoryRefs list which contain the Categories, that have a reference to the Item.

It is (3) and (4) which are the "twists". These are a bi-directional many-to-many relationship between Category and Item.

Inserting and updating poses no problem. The problem is in the delete of a Category. When a Category is deleted, it should delete all items it owns,
but not the items to which he only has a reference. So in effect: when a Category is deleted, the Items to which the Category has a reference, should remove the reference to the Category in their categoryRefs list.

I can't find the right cascade mode that should be set for the many-to-many relation from Category to Item.
When I set "save-update", the effect of "deleting" the category is not cascaded along the relation.

I also have written code so that at the moment of the deletion, the reference is removed in the Item object,
and this makes the Item dirty, but there is AFAIK no "auto-save-of-dirty-objects".

I guess I explicitly I have to call getSession().saveOrUpdate(item) to have it persisted and remove the reference, but this breaks
my "Hibernate-will-take-care-of-it" feeling (not to mention my simple DAO approach :) ).

Or is there a more straightforward "Hibernate"-solution to this?

These are the interesting parts of the mapping documents:

Item.hbm.xml:
-----------------
<set name="categoryRefs" lazy="true" table="CATEGORY_ITEM" cascade="save-update" inverse="false" >
<key column="ITEM_ID" />
<many-to-many class="Category" column="CATEGORY_ID"/>
</set>

Categorie.hbm.xml:
-----------------------
<set name="itemRefs" lazy="true" table="CATEGORY_ITEM" cascade="save-update" inverse="true">
<key column="CATEGORY_ID" />
<many-to-many class="Item" column="ITEM_ID"/>
</set>

BTW: good book.

Kind regards,

Jos


Top
 Profile  
 
 Post subject: Re: many-to-many delete cascade
PostPosted: Sun Jul 18, 2004 1:03 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
It sounds like you need to do this logic programmatically (deleting items with no non-deleted categories). This should be easy if you are deleting one at a time...

BTW Hibernate automatically saves dirty objects, try doing a transaction.commit() without explicity giving it an object reference. You only need to send the object reference if it is from a different session.

http://www.gloegl.de/15.html (CTRL-f 'automatically')

Chris


Top
 Profile  
 
 Post subject: Re: many-to-many delete cascade
PostPosted: Sun Jul 18, 2004 6:14 am 
Newbie

Joined: Sat Jul 17, 2004 5:13 pm
Posts: 8
Location: Edegem, Belgium
Hello,

I must have had a very cloudy day yesterday....

This morning, I rewrote the code so that I ask the items to remove the reference to the Category and indeed they are automatically saved without explicit call to saveOrUpdate(). So there is such a thing as "auto-save-of-dirty-objects". I probably didn't make it dirty enough :-)

It works well know. Thanks for the hint.

Jos


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.