-->
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 Issue. How to store link to the last child?
PostPosted: Thu Jul 15, 2004 1:14 pm 
Newbie

Joined: Thu Jul 15, 2004 11:43 am
Posts: 1
Hello I'm having a problem with a parent child relationship where the parent also stores a link to the most recent child added.

The Schema looks like this:

Parent
id
Children
LastChild

Child
id
Parent

Here is the hibernate modeling information for the parent class:

/**
* Returns a set of all Children
* @hibernate.set table="child" lazy="true" cascade="all" inverse="true"
* @hibernate.collection-key column="parent_id"
* @hibernate.collection-one-to-many class="com.kshep.Child"
*/
public Set getChildren() {
return m_children;
}

/**
* Returns the last child added to the children list
* @return the last child added to the children list
* @hibernate.many-to-one column="last_child_id" cascade="save-update"
*/
public Child getLastChild() {
return m_lastChild;
}

Here is how I've currently modeled the child class
/**
* Gets the parent object
* @hibernate.many-to-one non-null="true" column="parent_id" cascade="none"
*/
public Parent getParent() {
return m_parent;
}


The SQL generates exactly the way I want it. The problem comes when I try to delete a Parent object. When deleting the parent, first it tries to delete all of the Children since the Set is an inverse relationship. The problem is that when it tries to delete the children it violates the foreign key constraint on the lastChild column since lastChild would be pointing to an ID that no longer exists... Is there a way to make hiberate automatically null that field out before it tries to delete the children? I supposed I could null it out myself, but in my case the parent is also a child of another different parent class.

It just seems like this should be a pretty common problem, but I didn't see anything in the FAQ or forums that handle this specific case. If I get rid of the lastChild column relationship everything works fine. The reason I'd like this is because the lastChild will get references quite frequently and the Set of Children belonging to one parent is going to grow large. So I wanted to avoid having to load the entire Set just to find the latest Child.

Any help would be greatly appreciated.

Thanks,

-Keith


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 15, 2004 10:26 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
To help us understand your problem you need to post more details.

It seems that you have circular referencing. The parent owns children, but one of these children (last child) owns the parent. One solution is to make the parent own the last child and not the reverse.

mota


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.