-->
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.  [ 4 posts ] 
Author Message
 Post subject: Parent/child relationship: same table = problems deleting
PostPosted: Tue Dec 07, 2004 1:19 pm 
Newbie

Joined: Tue Dec 07, 2004 12:44 pm
Posts: 13
Hi! I have a parent/child relationship in the same table. When an item is deleted, I want all its children to be deleted, too. If I delete an item with no children, all works fine, but when the item has one subitem or more, it complains with this exception:

ERROR: update or delete on "cm_category" violates foreign key constraint "fka01e3933afbcd1cf" on "cm_category"

The database table is as follows:

CREATE TABLE cm_category
(
id int8 NOT NULL,
id_parent_category int8,
CONSTRAINT cm_category_pkey PRIMARY KEY (id),
CONSTRAINT fka01e3933afbcd1cf FOREIGN KEY (id_parent_category) REFERENCES cm_category (id) ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH OIDS;

And here is the mapping:

<hibernate-mapping>
<class
name="mycontent.manager.Category"
table="cm_category"
proxy="mycontent.manager.Category"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="native">
</generator>
</id>

<set
name="children"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
>

<key
column="id_parent_category"
/>

<one-to-many
class="mycontent.manager.Category"
/>
</set>

<many-to-one
name="parent"
class="mycontent.manager.Category"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="id_parent_category"
/>


</class>

</hibernate-mapping>

If I set inverse="false" then the item is deleted but its children are left with no parent. What's wrong here?

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 07, 2004 1:56 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Please follow the chapter about parent/child relationships in the reference documentation step by step.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 09, 2004 8:02 am 
Newbie

Joined: Tue Dec 07, 2004 12:44 pm
Posts: 13
christian wrote:
Please follow the chapter about parent/child relationships in the reference documentation step by step.


I had followed that chapter step by step already, I swear ;)

I found the problem. I had a "BeanUtils.copyProperties" lost in my code to copy the data from the from to the persistent object (I'm using Struts), and a converter that empties the sets (to prevent form objects from reaching the persistence layer when I do a save). The object that I wanted to delete was beign passed to session.delete() with its children set empty, so hibernate couldn't perform the cascade deletion, and then postgresql was complaining about it. I don't need that "BeanUtils.copyProperties" call at all when doing a delete, now it works fine.

Thanks anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 09, 2004 8:27 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
BeanUtils.copyProperties
is really bad, you don't know exactly what is happening when you call this things.
Are you using DTO? if yes, sorry for you ;(

Next project try to use your persistent objects directly with a long session per application transaction, you'll have less errors, more control et less code.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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