-->
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.  [ 2 posts ] 
Author Message
 Post subject: Novice having problems with map and entity deletion
PostPosted: Thu Nov 16, 2006 12:46 pm 
Newbie

Joined: Fri Feb 20, 2004 2:33 pm
Posts: 3
Location: Valencia, Spain
Hi, maybe this is easily solved but I can't find any information:
I am progamming a website with categories. Each category shall
have a map/list of related categories. So I wrote a class category
with Map relatedCategories and provided the following mapping:

Mapping document:
<hibernate-mapping>
<class name="Category" table="Category"
entity-name="Category">
<id name="id" column="CategoryID">
<generator class="native" />
</id>

<property name="name" column="CategoryName"/>
<property name="icon" column="CategoryIcon"/>
<property name="description" column="CategoryDescription"/>
<property name="siteID" column="SiteID"/>

<map name="relatedCategories" table="CategoryMapping" lazy="true" cascade="all">
<key column="CurrentCategoryID"/>
<index column="KeyName" type="string"/>
<many-to-many column="RelatedCategoryID" class="Category"/>
</map>

</class>
</hibernate-mapping>

So this means that I have a mapping table CategoryMapping that
maps a category CurrentCategory with a map of Relatedcategories.
Adding, modifying and removing related categories works fine.
But what if I delete a category which is a relatedCategory of one or
more other categories? How can I tell hibernate to remove all
rows of CategoryMapping whose foreign key RelatedCategoryID points to the removed category?


Top
 Profile  
 
 Post subject: Think I solved it myself
PostPosted: Fri Nov 17, 2006 4:59 pm 
Newbie

Joined: Fri Feb 20, 2004 2:33 pm
Posts: 3
Location: Valencia, Spain
I added a second map:

<map name="categoriesThatRelateToThisCategory" table="CategoryMapping" lazy="true">
<key column="RelatedCategoryID"/>
<index column="KeyName" type="string"/>
<many-to-many column="CurrentCategoryID" class="Category"/>
</map>

so that each category now has a map with all categories that
relate to it. Also I removed the cascade attribute in both maps.
This way when I delete an entity category, only the corresponding
rows in CategoryMapping are deleted as well but not the related
categories. Seems to work fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.