-->
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: how to disassociate both ways in bidirectional many-to-many
PostPosted: Mon Aug 30, 2004 11:49 am 
Beginner
Beginner

Joined: Fri Aug 29, 2003 10:01 am
Posts: 34
Location: florence, italy
Hibernate version: 2.1

Mapping documents:
Code:
<class name="Category" table="CATEGORY">
   ...
   <list name="items"table="CATEGORY_ITEM" lazy="true" cascade="save-update">
   <key column="CATEGORY_ID"/>
   ..
   <many-to-many class="Item" column="ITEM_ID"/>
   </set>
</class>


<class name="Item" table="ITEM">
   ...
   <bag name="categories" table="CATEGORY_ITEM" lazy="true" inverse="true" cascade="save-update">
   <key column="ITEM_ID"/>
   <many-to-many class="Item" column="CATEGORY_ID"/>
   </bag>
</class>


The mapping above is similar to p. 227 of "the bible". Notice that no cascade="delete" is set, as I simply want to disassociate Categories and Items, from both ends, and not delete in cascade.

If I delete a Category referred by an Item, the mn table CATEGORY_ITEM is updated ok, and no exception is thrown.
But if I delete an Item which is referred by a Category, the database throws a foreign key reference violation.

I found a workaround: simply do not put
Code:
inverse="true"
in the mapping, but the all the rest as in the inverse case: this way the mn table CATEGORY_ITEM is updated two ways
(but in this case the primary keys created by the schema tool on the mn table CATEGORY_ITEM may be the wrong ones, as the mapping order becomes relevant).

I wonder whether there is another way to do this, as this is clearly a trick.


Top
 Profile  
 
 Post subject: correction and integration
PostPosted: Tue Aug 31, 2004 6:01 am 
Beginner
Beginner

Joined: Fri Aug 29, 2003 10:01 am
Posts: 34
Location: florence, italy
after a nigth of meditation, I must make:

- a correction: the schema is created correctly, using bag as the inverse of the list

- an integration: this is not a trick, but the correct solution: with this solution when deleting an Item (or a Category), deletion of the association (but not of the associated) is performed automatically by Hibernate. Even by introducing a Composite, you will not get this


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 3:00 pm 
Beginner
Beginner

Joined: Fri Aug 29, 2003 10:01 am
Posts: 34
Location: florence, italy
well, seeing it now this is quite misleading: for a proper model see the caveatemptor Categorizeditem persistence. the way proposed above is both "working against the framework", and using an object model less fine grained than the relational one, two bad sins.


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.