-->
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: Parent child relation in same table
PostPosted: Mon Oct 22, 2007 12:03 pm 
Beginner
Beginner

Joined: Sun Feb 19, 2006 5:13 am
Posts: 22
Hi


I have a Comment class, which has one or more child comments:


<class name="com.fi.cruds.api.comment.Comment" table="Comment" dynamic-update="true" lazy="false">

<id name="id" access="field" type="java.lang.Long" unsaved-value="null">
<generator class="native"/>
</id>
<version access="field" name="version" unsaved-value="null"/>
<many-to-one name="parentComment"
column="parentId"
access="field"
class="com.fi.cruds.api.comment.Comment"
not-null="false"
cascade="none"
lazy="false"/>
<set name="childComments"
inverse="false"
cascade="all-delete-orphan"
access="field"
lazy="false">
<key column="parentId"/>
<one-to-many class="com.fi.cruds.api.comment.Comment"/>
</set>
</class>

As you see I have cascade="all-delete-orphan" on the children, which means that if I delete a comment all children should be removed. Also I would like to have inverse="true" to have the parent own the relationship but I dont have it here because of reasons further down.

Ok so evrything connected to saving works perfect but the delete does not when I have invers="true". I get a database constraint on the parentId, which for me is strange. If idelete a Comment shouldn hibernate remove all children automatically also? Or do I have to get the parent and remove the comment when deleting OR session.delete if it is a root comment.
There are no really good input on having parent/children for same class in same table so I hope someone has a comprehensive examples of this.


Cheers
Magnus


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 23, 2007 11:03 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
Not sure but I think that when you set inverse attribute to true, you specify that the child objects (or other end of association) is responsible for maintaining relationship.

Isn't saving / deleting working fine with no inverse attribute in childComments and inverse="true" in parentComment?

As per hibernate documentation

"The rules you have to remember are straightforward: All bi-directional associations need one side as inverse. In a one-to-many association it has to be the many-side, in many-to-many association you can pick either side, there is no difference."

_________________
Please rate this post if you find it helpful


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.