-->
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: Deleting from a many-to-many self-referential relationship
PostPosted: Fri Feb 03, 2006 1:09 pm 
Newbie

Joined: Wed Jan 25, 2006 7:47 pm
Posts: 12
Hi everyone,

As the header says, I have a bi-directional, many-to-many self-referential association. The relationship is stored in a join table with a composite primary key of the 2 foreign keys. My hibernate mapping is similar to this example:

Code:
<class name="ProductClass" table="PRODUCT_TABLE">

    <id column="PRODUCT_ID".../>

    <set name="primaryProducts" lazy="true" table="PRIMARY_ASSOCIATE_PRODUCTS_TABLE">
        <key column="PRODUCT_ID"/>
        <many-to-many class="ProductClass" column="ASSOCIATE_PRODUCT_ID"/>
    </set>

    <set name="associateProducts" inverse="true" lazy="true" table="PRIMARY_ASSOCIATE_PRODUCT_TABLE">
        <key column="ASSOCIATE_PRODUCT_ID"/>
        <many-to-many class="ProductClass" column="PRODUCT_ID"/>
    </set>

</class>

So I retrieve an instance of Product, and make it transient:

Code:
ProductClass thisProduct = findById(product_id);
makeTransient(thisProduct);
commit;


My question is, can I expect Hibernate to find & delete all instances of thisProduct where it is EITHER in the set of primaryProducts or associateProducts (i.e., in either column of PRODUCT_ASSOCIATE_PRODUCT_TABLE)? What's happening now is that thisProduct and all relationships where thisProduct is the primaryProduct are being deleted (the row in PRODUCT_ASSOCIATE_TABLE is deleted). But if thisProduct is the associateProduct, it remains in the PRIMARY_ASSOCIATE_PRODUCT_TABLE and is now orphaned, because the primary key in PRODUCT_TABLE is no longer there.

That does seem like a lot of logic to expect from Hibernate, but how is it even able to delete the PRODUCT_TABLE entry if there are still references to it as an associateProduct? I would think that a cascade option would be necessary, but the docs says that isn't needed for many-to-many relationships.

Thanks for any input.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 10:43 am 
Newbie

Joined: Wed Jan 25, 2006 7:47 pm
Posts: 12
Ok, so has no one responded because my question is too hard, or the answer is too easy??


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 5:40 am 
Newbie

Joined: Thu Jan 08, 2004 5:11 am
Posts: 4
Location: France
I think, if you simply add
Code:
cascade="all,delete-orphan"
on your sets, this shoud do the work.


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.