-->
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.  [ 13 posts ] 
Author Message
 Post subject: How do I duplicate a hibernate object?
PostPosted: Thu Jun 09, 2005 5:50 pm 
Newbie

Joined: Sun May 15, 2005 1:24 pm
Posts: 11
Hi,

What is the easiest way to duplicate a hibernate object including all of its childrens and update it into the database?

THanks a lot.
Henry


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 1:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
replicate?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 1:49 am 
Newbie

Joined: Sun May 15, 2005 1:24 pm
Posts: 11
max wrote:
replicate?

Thanks for reply.

It is not replicate. What I need is to make a new hibernate object with a different "id"(primary key), but all other properties are copied from the orignial.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 5:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
session.replicate() and then nuke the version info manually

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 6:19 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
hi

I have the same problem and I can't solve it using replicate. BTW why to use replicate? Replicate is for making detached object persistent, isn't it?

Code:
Foo foo = session.get(Foo.class, fooId);
// here I'd like to read all properties of the lazy object (what method does that?)
session.evict(foo);
foo.setId(null);
session.save(foo);


this throws an exception:
Code:
ERROR AssertionFailure - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: null identifier
   at org.hibernate.engine.EntityKey.<init>(EntityKey.java:33)
   at org.hibernate.engine.PersistenceContext.reassociateProxy(PersistenceContext.java:518)
   at org.hibernate.engine.PersistenceContext.unproxyAndReassociate(PersistenceContext.java:565)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:65)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
   at org.hibernate.engine.Cascades$5.cascade(Cascades.java:154)
   at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771)
   at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
   at org.hibernate.engine.Cascades.cascade(Cascades.java:847)
   at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:332)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:214)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
   at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
   at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
essionImpl.java:476)


Please provide a simple example how to clone a persistent object under a new (generated) id.

Thanks in advance
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 6:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
sorry yes - my bad. wrong usage of replicate.

Don't have any good solutions at hand then...

-max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 10:30 am 
Newbie

Joined: Wed Mar 02, 2005 11:14 am
Posts: 4
max,

Would be possible to generate the POJOs with Velocity that implement Cloneable?

If so has anybody written these templates as I'd love to get them too!

Otherwise I have to modify the POJOs by hand but they get over written when I generate the code and the code generated classes can't form part of my build.

Regards,
Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 8:01 am 
Beginner
Beginner

Joined: Tue Jun 21, 2005 8:38 am
Posts: 37
zdila wrote:
hi

I have the same problem and I can't solve it using replicate. BTW why to use replicate? Replicate is for making detached object persistent, isn't it?

Code:
Foo foo = session.get(Foo.class, fooId);
// here I'd like to read all properties of the lazy object (what method does that?)
session.evict(foo);
foo.setId(null);
session.save(foo);


this throws an exception:
...

Please provide a simple example how to clone a persistent object under a new (generated) id.


You many want to check out:
http://hansonchar.blogspot.com/2005/06/ ... rnate.html

Using beanlib, you can do something like:

Code:
Foo foo2 = HibernateBeanReplicator.dupEntityBean(foo);
foo2.setId(null);
session.save(foo2);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 8:19 am 
Newbie

Joined: Mon Oct 18, 2004 7:59 am
Posts: 10
Location: Netherlands
zdila wrote:
hi

I have the same problem and I can't solve it using replicate. BTW why to use replicate? Replicate is for making detached object persistent, isn't it?

Code:
Foo foo = session.get(Foo.class, fooId);
// here I'd like to read all properties of the lazy object (what method does that?)
session.evict(foo);
foo.setId(null);
session.save(foo);


Instead, if you do
Code:
session.update(foo);
the object should get saved with a new id. This is not really cloning though, the "same" object is saved to the database twice.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 04, 2005 12:38 pm 
Beginner
Beginner

Joined: Thu Jul 08, 2004 2:21 pm
Posts: 20
Location: Toronto
It is easy to do with org.apache.commons.beanutils

It looks something like that:

import static org.apache.commons.beanutils.PropertyUtils.*;

....
// retrieve an object
Foo persistedFoo = session.get(Foo.class, myFooId);
// create a new instance
Foo newFoo = new Foo();
// copy properties from persisted object to a new one
copyProperties(newFoo, persistedFoo);
...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 01, 2008 7:09 am 
Newbie

Joined: Tue Jul 01, 2008 6:59 am
Posts: 1
Hello

You can't use BeanUtils.copyproperties because the result is a reference (a new a pointer) to first object, so you have two references (or pointers) that are pointing to the same thing. This is a common issue of java language. If you change a property using a pointer, other pointer can see the changes of these property.

We need duplicate properties but the properties have to be new objects themselves: same content but diferent identity! (equals and hashcode issue)

Thanx!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 01, 2008 9:09 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
be advised do analyze that u need a deep copy or a shallow copy of the pojo/persistent object


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 11:01 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Indeed, make sure that when you do a session.replicate() , you are properly copying all associated proerties, including ones that might potentially be loaded in a Lazy fashion.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.