-->
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: how to copy an object with a new id
PostPosted: Fri Apr 09, 2010 12:42 pm 
Newbie

Joined: Thu Mar 25, 2010 7:55 am
Posts: 8
Hi!

I have found a few answers to this, but I kindof did not get it:

I need to make a copy of an entity, change a few values and then save that object with a new id (resulting in an insert rather than an update). Maybe my problem is that the id is a long (and generated in the database)

I cant implement a .clone method.

If I "evict" the original object, and save it, it will result in an error because the id (obviously) is already in use. If I change the id (which is a long) manually to "0" or "-1" (which I thought would be hibernate for "new") hibernate will just save it with the id "0" and the next iteration obviously that will through an error.

So, how can do that, dont get it!??

Thanks a loads!

Cheers!


Top
 Profile  
 
 Post subject: Re: how to copy an object with a new id
PostPosted: Sun Apr 11, 2010 1:42 pm 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I would simply get the object, create a new object, get the data from the first and set the data in the new.
Don't touch the id, so it will stay to the default. I think then it should make a new id when inserting in the DB.


Top
 Profile  
 
 Post subject: Re: how to copy an object with a new id
PostPosted: Sun Apr 11, 2010 3:06 pm 
Newbie

Joined: Thu Mar 25, 2010 7:55 am
Posts: 8
Hey mate, thanks for your answer!

Do you think there is a way without having to copy the data from one object to the other? Those objects have about 20 fields each, and there are about a dozen. Thats a lot of not so nice code!

Thanks!
CC


Top
 Profile  
 
 Post subject: Re: how to copy an object with a new id
PostPosted: Sun Apr 11, 2010 3:09 pm 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
Don't know if Hibernate can help you, but you could make a simple method to do it.


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.