-->
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: Cannot delete a parent object with bidirectional child refs!
PostPosted: Sun Mar 11, 2007 2:57 am 
Newbie

Joined: Sun Mar 11, 2007 2:23 am
Posts: 2
Hi,

I've been trying to get the parent/child example from chapter 21 of the reference documentation working without success. It seems very straight forward so I'm probably missing something obvious but I've spent the last few hours reading the forum and searching online without finding the answer.

The mapping for the parent object is:

<class name="com.test.Parent" table="PARENTS">
<id name="id" column="parent_id">
<generator class="assigned"/>
</id>
<property name="name" not-null="true"/>
<set name="children" inverse="true" cascade="all,delete-orphan">
<key column="parent_id"/>
<one-to-many class="com.test.Child"/>
</set>
</class>

The mapping for the child object is:

<class name="com.test.Child" table="CHILDREN">
<id name="id" column="child_id">
<generator class="assigned"/>
</id>
<property name="name" not-null="true"/>
<many-to-one name="parent" column="parent_id" not-null="true"/>
</class>

I can create the objects by adding a child to the parent and saving the parent. I can even delete the children and the parent IF it has no children BUT if I try to delete a parent object that does have children I get the following error:

Duplicate key or integrity constraint violation message from server: "Cannot delete or update a parent row: a foreign key constraint fails"

I would expect Hibernate to delete the children with the parent because I have specified cascade="all,delete-orphan" but the logs show that Hibernate is not trying to delete (or even update) the child at all. The first SQL call is the delete on the Parent table and it of course fails.

I've tried a number of different settings (too many to list here) but nothing has changed this behavior.

I'm also seeing the same problem for an object with a collection of values (strings) as described in section 1.3.4 of the reference documentation.

I am using Hibernate 3.2 with MySQL 4.1 on windows.

I'd appreciate any assistance anyone could offer.

Thanks
Sam


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 11, 2007 3:34 am 
Newbie

Joined: Sun Mar 11, 2007 2:23 am
Posts: 2
Ok never mind people, I forgot that I was writing the delete query manually to avoid retrieving the object and sending it to session.delete(). Seeing as Hibernate would have to get the object to remove it's children anyway I'll change my code to do it the proper way.

Boy is my face red.

:p


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.