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 deleting
PostPosted: Fri Jan 09, 2009 2:57 am 
Newbie

Joined: Thu Dec 25, 2008 7:13 am
Posts: 10
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

2.0.1.4

Mapping documents:

Multiple mapping files

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="FlexiCommerce.Components.PersistenceItem,FlexiCommerce" table="FlexiItem">
<cache usage="read-write" />

<id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
<generator class="native" />
</id>

<discriminator column="Type" type="String" />
<property name="Name" access="property" length="255" />
</class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<subclass name="FlexiCommerce.Components.Category,FlexiCommerce" extends="FlexiCommerce.Components.PersistenceItem,FlexiCommerce" discriminator-value="Category" lazy="false">

<bag name="Products" lazy="false" generic="true" table="Flexi_ProductCategoryMapping" inverse="true">
<cache usage="read-write" />
<key column="ID2"></key>
<many-to-many class="FlexiCommerce.Components.Product,FlexiCommerce" column="ID"></many-to-many>
</bag>

</subclass>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<subclass name="FlexiCommerce.Components.Product,FlexiCommerce" extends="FlexiCommerce.Components.PersistenceItem,FlexiCommerce" discriminator-value="Product" lazy="false">
<bag name="Categories" generic="true" lazy="false" table="Flexi_ProductCategoryMapping">
<cache usage="read-write" />
<key column="ID"></key>
<many-to-many class="FlexiCommerce.Components.Category,FlexiCommerce" column="ID2"></many-to-many>
</bag>

</subclass>

</hibernate-mapping>

What I'm trying to do.

Right now, when I delete a category, all the referenced products get delete, and all the categories referencing those products get deleted. What ends up happening is every category and product record in my database gets deleted.

What I would like to happen is when I delete a category or product, nothing else gets deleted. I set cascade="none" so the referenced items wouldn't inherit the deletes and it says it can't delete due to an enforced foreign key relationship (the many-to-many mapping table).

How do I get it so that when I delete a category or product, nothing else gets deleted besides all of its mappings in the mapping table?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 3:04 am 
Newbie

Joined: Thu Dec 25, 2008 7:13 am
Posts: 10
if there is no way to get this to work, maybe someone can show me how to make the foreign key relationship non-enforced.

I really need database independence and I'm not sure if other databases support that.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 3:13 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
What I would like to happen is when I delete a category or product, nothing else gets deleted. I set cascade="none" so the referenced items wouldn't inherit the deletes and it says it can't delete due to an enforced foreign key relationship (the many-to-many mapping table).

How do I get it so that when I delete a category or product, nothing else gets deleted besides all of its mappings in the mapping table?


Setting cascade to "none" should be the only thing you have to do. With the inverse mapping you probably also have to manually remove the products from the category bag.

_________________
--Wolfgang


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.