-->
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.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Copying Object State
PostPosted: Fri Jun 24, 2005 2:00 pm 
Newbie

Joined: Mon Nov 29, 2004 4:06 pm
Posts: 8
Greetings,
I am looking for a best practice approach to copying a state of an object in hibernate.

The use case is such that my users can select an existing object from a list for duplication. Everything except the id should be copied, since the database will be assinging the id.

I know I can brutally do this by calling setters and getter. Also, I know of Jakarata Commons BeanUtils.copyProperties(object1, object2). However, since I am using hibernate to manage my persistence, I am thinking their might be a better way. I have looked at the API, the only thing I think that might do it is "merge"?

Your suggestions are appreciated.

Curtney


Top
 Profile  
 
 Post subject: Copy Constructor?
PostPosted: Fri Jun 24, 2005 2:44 pm 
Newbie

Joined: Fri Jun 11, 2004 4:58 pm
Posts: 9
What about just implementing a copy constructor?


Top
 Profile  
 
 Post subject: Re: Copying Object State
PostPosted: Fri Jun 24, 2005 2:50 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
curtney wrote:
Greetings,
I am looking for a best practice approach to copying a state of an object in hibernate.

The use case is such that my users can select an existing object from a list for duplication. Everything except the id should be copied, since the database will be assinging the id.

I know I can brutally do this by calling setters and getter. Also, I know of Jakarata Commons BeanUtils.copyProperties(object1, object2). However, since I am using hibernate to manage my persistence, I am thinking their might be a better way. I have looked at the API, the only thing I think that might do it is "merge"?

Your suggestions are appreciated.

Curtney


You might want to read the docs on merge(). I don't think it's what you're looking for.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 8:05 pm 
Newbie

Joined: Mon Nov 29, 2004 4:06 pm
Posts: 8
I thought of the copy constructor and was going with that approach (Might still go with that approach) However, few of my objects have a lot of attributes. It would seem rather painful to provide a constructor that list them all, besides if I later change or add an attribute to my object I would have to modify the constructor as well, which is not big problem. But...

I was thinking of implenting clone, but I read somewhere that a copy constructor is a better alternative.

I guess merge is not it, I will re-read the description again.

Thanks.


_CJ


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 8:14 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
I'm still pretty new to hibernate, but I'll throw out an idea if you want to try it or if others want to comment on it:

Code:

(pseudocode)

MyObject ob = hibernate.get(idOfExisting)
hibernate.evict(ob)
ob.setId(0)
hibernate.save(ob)



Hibernate in Action says you shouldnt really use .evict() but it might be called for in this case, and seems like that might work.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 25, 2005 10:15 pm 
Newbie

Joined: Mon Nov 29, 2004 4:06 pm
Posts: 8
Actually, I have read several threads in this forum on this approach. However, none gave the impression that it worked. If it did work, it was only partially, there was some issues with evicting collections. My object also contain a few collections. It would be nice to also copy those as well.

Anyway, evict seems to be most promising method. I will be testing this out this weekend.

Has anyone out there use evict to successfully copy/clone (collection and all) an object?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 26, 2005 2:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The correct way is to implement your own generic shallowCopy() algorithm, making use of the Hibernate ClassMetadata and CollectionMetadata interfaces.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 11:52 pm 
Beginner
Beginner

Joined: Tue Jun 21, 2005 8:38 am
Posts: 37
You can use "beanlib", and provide your own customized "Vetoer" by implementing the BeanPopulatable interface to control the population of each bean property:

http://hansonchar.blogspot.com/2005/06/ ... rnate.html

For example, if you want to deep clone the whole object graph excluding the ID's:

Code:
   BeanPopulatable vetoer = new MyVetoer(session);
   // Deeply replicate the "fromBean" but not the ID's,
   // eagerly fetching as necessary
   Object toBean = HibernateBeanReplicator.dupEntityBean(fromBean, vetoer);

Code:
/** Not to populate ID's. */
public class MyVetoer implements BeanPopulatable {
   private SessionFactory sessionFactory;
   
   public MyVetoer(Session session) {
      this.sessionFactory = session.getSessionFactory();
   }
   
   public boolean shouldPopulate(String propertyName, Method readerMethod)
   {
      if (Collection.class.isAssignableFrom(readerMethod.getReturnType()))
         return true;
      // Skip populating if it is an identifier property.      
      Class c = readerMethod.getDeclaringClass();

      if (Enhancer.isEnhanced(c)) {
         // figure out the pre-enhanced class
         c = c.getSuperclass();
      }
      ClassMetadata classMetaData = sessionFactory.getClassMetadata(c);
      return !classMetaData.getIdentifierPropertyName().equals(propertyName);
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 29, 2005 7:55 pm 
Beginner
Beginner

Joined: Tue Jun 21, 2005 8:38 am
Posts: 37
To take care of non-Hibernate entity, siimply:

Code:
public class MyVetoer implements BeanPopulatable {
    ...
    public boolean shouldPopulate(String propertyName, Method readerMethod)
    {
       ...
       return classMetaData == null || !classMetaData.getIdentifierPropertyName().equals(propertyName);
    }
}
[/quote]


Top
 Profile  
 
 Post subject: Objects replication - How to use partial copy with beanlib ?
PostPosted: Wed Dec 14, 2005 9:33 am 
Newbie

Joined: Wed Dec 14, 2005 5:48 am
Posts: 4
Hi,

I find the library beanlib (2.4.2 coupled with Hibernate 3.0.5) nice and I think it feet exactly what I need. (I didn't not use beanlib 3.0.4 because I got a problem with class version)

However, I am facing a problem concerning a partial deep clone although I dig trough the source code.
I tried to use HibernateBeanReplicator.copy()" method with the following initializers "initCollectionPropertyNameSet" and/or "initEntityBeanClassSet" to specify which objects and properties from my domain I would like to replicate. But I still get the same behaviour: a huge replication as deepCopy method.

Does anyone have an idea ?

Thanks


Top
 Profile  
 
 Post subject: Re: Objects replication - How to use partial copy with beanl
PostPosted: Wed Dec 14, 2005 12:26 pm 
Beginner
Beginner

Joined: Tue Jun 21, 2005 8:38 am
Posts: 37
Thomas COUSIN wrote:
Hi,

I find the library beanlib (2.4.2 coupled with Hibernate 3.0.5) nice and I think it feet exactly what I need. (I didn't not use beanlib 3.0.4 because I got a problem with class version)

However, I am facing a problem concerning a partial deep clone although I dig trough the source code.
I tried to use HibernateBeanReplicator.copy()" method with the following initializers "initCollectionPropertyNameSet" and/or "initEntityBeanClassSet" to specify which objects and properties from my domain I would like to replicate. But I still get the same behaviour: a huge replication as deepCopy method.

Does anyone have an idea ?

Thanks


Can you post your class, and the code related to the initiCollectionPropertyNameSet to the beanlib's Sourceforge Help forum, so I can have a look at the problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 1:07 pm 
Newbie

Joined: Wed Dec 14, 2005 5:48 am
Posts: 4
Hi hanson,

This is the way I use the beanLib:

Code:
    Object replicateObject = new Object();
    BeanPopulatable vetoer = new MyVetoer(this.hibernateSessionProvider.getSession());
   
    Set setEntityBean = new HashSet();
    Class[] arrayEntityBean = {MySuperClass.class};
    setEntityBean.addAll(Arrays.asList(arrayEntityBean));
   
    Set setProperties = new HashSet();
    String[] arrayProperties = {"onePropertyNameFromMySuperClass", "otherPropertyNameFromMySuperClass"};
    CollectionPropertyName[]  tabCollectionPropertyName =     CollectionPropertyName.createCollectionPropertyNames(MySuperClass.class, arrayProperties);
    setProperties.addAll(Arrays.asList(arrayProperties));
   
    HibernateBeanReplicator replicator = new Hibernate3BeanReplicator()
                                                                    .initVetoer(vetoer)
                                                                    .initCollectionPropertyNameSet(setProperties)
                                                                    .initEntityBeanClassSet(setEntityBean);

    replicateObject = replicator.copy(object);


This code is wrapped in a method that I made just to test the lib and MyVetoer is basically, the one that you give in the previous post.

I can't reach SourceForge.net, the website seems to be down so sorry to continue here.

Hope this help you to understand what I am trying to do.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 2:51 pm 
Beginner
Beginner

Joined: Tue Jun 21, 2005 8:38 am
Posts: 37
See http://sourceforge.net/forum/message.php?msg_id=3475970


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 7:50 pm 
Newbie

Joined: Fri May 26, 2006 7:46 pm
Posts: 3
I have the same problem. I tried using beanLib. But this was implemented on JDK 5. On my current project I use JDK1.4. Has anyone have the solution?

Thanks in advance.
Udai.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 7:50 pm 
Newbie

Joined: Fri May 26, 2006 7:46 pm
Posts: 3
I have the same problem. I tried using beanLib. But this was implemented on JDK 5. On my current project I use JDK1.4. Has anyone have the solution?

Thanks in advance.
Udai.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next

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.