-->
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: auto deletion of foreign key records when deleting record
PostPosted: Thu Oct 12, 2006 6:38 am 
Newbie

Joined: Thu Oct 12, 2006 6:31 am
Posts: 2
Hi
I am working with Hibernate 3.1 and have the following doubt:
I have two tables, A and B.
B holds a field with foreign key to table A.
Is there a way to tell hibernate that when a request comes to delete a record from A, it will delete all records from B with the matching foreign key first?
I am trying to save the effort of checking and deleting by my self

10x


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 8:47 am 
Newbie

Joined: Thu Oct 12, 2006 7:39 am
Posts: 4
Hi

I assume, you have Table/Class B defined as a Set in the .hbm.xml of Table/Class A.

Therefore you can try

cascade="all-delete-orphan" in the <set/> tag.

as described on p. 111 of "Hibernate in action".

Greets

Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 9:24 am 
Newbie

Joined: Thu Oct 12, 2006 6:31 am
Posts: 2
Already tried that...
It gives not not-null property references a null or transient value:B
here is my actual hbm's:
Class Item has a class ItemData
Class ItemData has a class Structure
Class Structure has a class Field
Class Field has a BASED_ON_ITEM_ID field which has a foreign key, not null constraint to Item.item_id

When I delete Item I get the error:not not-null property references a null or transient value:Field.based_on_item

Class Structure:
<list name="fields" lazy="false" fetch="subselect" cascade="all,delete-orphan">
<cache usage="read-write"/>
<key column="STRUCTURE_ID"/>
<index column="INDEX" type="long"/>
<one-to-many class="Field"/>
</list>


Class B:
<many-to-one name="basedOnItem" class="Item" column="BASED_ON_ITEM_ID" not-null="true" lazy="false" foreign-key="FIELD_FK1"/>


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.