-->
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: How to copy an object with Hibernate ?
PostPosted: Thu Jul 19, 2007 7:54 am 
Beginner
Beginner

Joined: Wed Oct 25, 2006 9:05 am
Posts: 40
Location: France, Lieusaint
I am working on a webapp and facing the problem of cloning an object.


The scenario is simple:

I lazy-load a bean (Object) and want to copy all properties of this object, explicitly a creation by copy, then modify this detached object and finally make it persistent.

In fact, I realize I need to do a deep copy of the objet.

Is there a 'best way' to do that or should I simply do a deep copy of original object ?

thx


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 12:28 pm 
Regular
Regular

Joined: Sat Jan 22, 2005 6:57 pm
Posts: 50
Location: Chicago
I am not sure what you mean by "lazy load the bean." If you mean "lazy load" some of the attributes inside the bean, then you need to decided if those lazy loaded elements are to be part of your deep copy.

If they are to become part of the deep copy, there is no need to lazy load anything because you are going to require the data anyways.

You will need to make sure that each of the objects, including all of the complex attributes have a clone() method and implement the interface Cloneable. I would also null out the association between the parent and child attributes. For instance, if you have the following two classes:

Code:
public class Parent implements Cloneable {
    private String key;
    private String name;
    private Child child;
     
     // getters and setters

     public void setChild(Child child) {
           this.child = child;
           this.child.setParent(this);
     }
   
     public Object clone() {
           Parent ret = new Parent(this);
           ret.setChild((Child)child.clone());
           return ret;
     }
}


public class Child implements Cloneable {
      private String key;
      private String name;
      private Parent parent;

      // getters and setters

      public Object clone() {
            Child child = new Child(this);
            child.setParent(null);
            return child;
      }
     
}


You need to make sure that the child clone sets its parent as the new cloned parent instance.

If this helps, don't forget to rate! :)


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.