-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to make hibernate insert an object and not update it
PostPosted: Wed Nov 05, 2003 4:22 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi

I wish to create a class based on a table with an id that everytime I save an object based on the class instead of updating the row in the table it creates a new row with a new Id.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 4:25 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I do not understand why you may need this... Why not just

obj = obj.clone();
obj.setId(null);
session.save(obj);

?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 7:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://forum.hibernate.org/viewtopic.php?t=924779&highlight=clone

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Can't find replicate in API
PostPosted: Thu Nov 06, 2003 10:08 am 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi

I checked out the other forum and is exactly the same problem. I know I can clone the objects but that means that I have to clone also the references. I read that there was a replicate method for this kind of problem but I can't find it in the API.

Thanks for the help


Top
 Profile  
 
 Post subject: Re: Can't find replicate in API
PostPosted: Thu Nov 06, 2003 11:05 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
nestorjb wrote:
I read that there was a replicate method for this kind of problem but I can't find it in the API.

It's on the 2.1 version.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: The 2.1 version is in beta
PostPosted: Thu Nov 06, 2003 11:36 am 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi

2.1 is in beta right now. So the only workaround to this problem is to clone the objects?. Are there any other workaround.

Thanks for the help


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2003 7:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
As dimas said, does this work ?
Code:
myObj = session.load(...);
session.evict(myObj); // may not be useful
myObj.setId(null); //null assuming id is not primitive ; may not be useful because of explicit save usage
session.save(myObj);

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Setting the Id to null does not work
PostPosted: Fri Nov 07, 2003 12:54 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi

Setting the Id column to null does not work. I get the following exception:

net.sf.hibernate.HibernateException: identifier of an instance of WhateverClass altered from 1020020 to null

Thanks


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