-->
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.  [ 4 posts ] 
Author Message
 Post subject: Making copy of persistent object
PostPosted: Mon Aug 02, 2004 10:42 am 
Newbie

Joined: Mon Jun 07, 2004 9:08 am
Posts: 5
How can I make a copy of an existing data object, changing only the PK value?

For example,

Class A {
Long id; //is PK
String value;
......
}


This is essentially what I want, but it obviously doesn't work because of the way Hibernate's sessions work. Hibernate identifies the object as already existing in the session cache, and doesn't like changing the key.

aInstance = session.load(A.class,new Long(1));
aInstance.setID(new Long(2));
session.save(aInstance);


Any Ideas? How about if I do an evict(aInstance) right after I load it? Can I load an object without actually having Hibernate store it in the cache?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 4:02 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
Hibernate does not allow changing the id of a persistent object for obvious reasons. You can copy a persistent object by creating a transient object (e.g. with new) and setting/copying its attributes, including the id.

mota


Top
 Profile  
 
 Post subject: Re: Making copy of persistent object
PostPosted: Mon Aug 02, 2004 7:29 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
JavaJohnson wrote:
How can I make a copy of an existing data object, changing only the PK value?


Well... just clone your object or define a copy-constructor. You can then safely change its id without disturbing Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 7:33 am 
Newbie

Joined: Mon Jun 07, 2004 9:08 am
Posts: 5
Thanks. I figured it would come down to doing it manually, I just wanted to ask to see if there was something out there already. The cloning becomes more complicated when there are associations mapped in the class as well, but nothing I can't handle!

Thanks again.


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