-->
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: Adding a new child to an entity removes the persisted one
PostPosted: Thu Nov 19, 2009 7:02 am 
Newbie

Joined: Tue Aug 15, 2006 8:51 am
Posts: 1
Hi all,
I have a simple parent-child relationship:

The parent class:
Code:
@Entity
public class Parent implements Serializable {
   @Id
   @GeneratedValue
   @Column(name = "PARENT_ID")
   public long getId() {
      return id;
   }

// some other fields

   @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
   @JoinTable(name = "PARENT_CHILD",
           joinColumns = { @JoinColumn(name = "PARENT_ID") },
           inverseJoinColumns = { @JoinColumn(name = "CHILD_ID") })
   public Set<Child> getRelatedChildren() {
      return relatedChildren;
   }
}


...and the child class:
Code:
@Entity
public class Child implements Serializable {

   private static final long serialVersionUID = -46484016106062423L;
   private long id;
   
   /**
    * @return the id
    */
   @Id
   @GeneratedValue
   @Column(name = "CHILD_ID")
   public long getId() {
      return id;
   }

// some other fields
}


Now I get a Parent object from the DB, give it to the real world and get it back with some added children. Then I give this to my DAO for updating the existing one:

Code:
   public void update(Parent parent) {
      Parent parentFromDB = getParentById(parent.getId());
      Set<Child> relatedChildrenFromDB = parentFromDB.getRelatedChildren();
      
      // merge the new picture from incoming parent into the entity
      relatedChildrenFromDB.addAll(parent.getRelatedChildren());
      parentFromDB = (Parent)getCurrentSession.merge(parentFromDB);
   }


Now, the new child is persisted, but all previous entries for this relation are thrown away. What is the trick to get it all together???

TIA,
Ralf


Top
 Profile  
 
 Post subject: Re: Adding a new child to an entity removes the persisted one
PostPosted: Thu Nov 19, 2009 7:48 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Try saving the parent object directly without manipulating the one from the db

Something like this:
Code:
public void update(Parent parent) {
       // merge the new picture from incoming parent into the entity
           getCurrentSession.merge(parent);
}


I feel this should work since the parent object that you get as parameter is just a detached instance of the object you loaded before passing it on to the real world. It is enough if you attach it back to the session.

_________________
Sukirtha


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.