-->
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 relationship problem
PostPosted: Wed Dec 20, 2006 12:43 pm 
Newbie

Joined: Wed Dec 20, 2006 12:35 pm
Posts: 1
I have a question about object semantics and implementing parent-child relationships in Hibernate. Parent has a list of children, and Child has a reference to its parent.

The problem is: the Parent's collection of children does not actually contain the Child object; it contains a copy. When I modify the Child, the same Child in Parent's collection of children does not get updated. This defies my expectations about how Hibernate should work. Is it possible I have the mappings incorrect?

Example:
Code:
Child child = childDao.findById(id);
child.setProperty(value);
....
childDao.saveChild(child); // save child

// Get all the parent's children
List children = parent.getChildren();


The List 'children' still contains the version of child before the modification above.

Here is the mapping in Parent:
Code:
   /**
    * @hibernate.bag name="children" lazy="false" cascade="all"
    * @hibernate.collection-key column="parent_id"
    * @hibernate.collection-one-to-many
class="com.myproject.model.Child"
    */
   public List getChildren() {
       return this.children;
   }

   public void setChildren(List children) {
       this.children = children;
   }


In Child:

Code:
   /**
    * @hibernate.property column="parent_id"
    * not-null="true"
    */
   public int getParentId() {
       return this.parentId;
   }

   public void setParentId(int parentId) {
       this.parentId = parentId;
   }


I have tried with and without the following mapping in Child:

Code:
  /**
    * @hibernate.many-to-one class="com.myproject.model.Parent"
cascade="none"
    * column="parent_id" update="false" insert="false"
    */
   public Parent getParent() {
       return this.parent;
   }

   public void setParent(Parent parent) {
       this.parent = parent;
   }


Any ideas what's wrong? Perhaps I have missed something fundamental about the way Hibernate works? I would expect Hibernate queries for the same database objects to yield equal (==) object references; is this not the case?

Thanks,
Nate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 4:12 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
look in the logs. see if the updated child is getting saved when you call save. better yet, try to update it and see if that is getting called before that load command runs.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


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.