-->
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.  [ 3 posts ] 
Author Message
 Post subject: saving same object multiple times
PostPosted: Sat Jul 07, 2007 5:01 am 
Newbie

Joined: Sat Jul 07, 2007 4:54 am
Posts: 2
this is probably a stupid question.. but is there any way to force hibernate to do an insert instead of an update when doing multiples saves of 1 object?

for example.. i instantiate my object and save it. now depending on some other factors i might change 1 field in this object and save it again (and rinse/repeat many times). when i do this i get updates after the first save. i would like to force it to keep inserting new rows instead.

is this possible? i would like to avoid having to create new objects over and over and propogate the many fields each time (when im just changing 1 field to the original).

hope my question makes sense.


thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 08, 2007 3:46 pm 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
Does nullifying entity id work?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 08, 2007 5:00 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
It's possible although it would be more consistent to clone this object since this is what you want to achieve in the principle.

You need two things :
1) check the entity is not managed by an open session. If so, do a session.evict(thisEntityInstance) to remove this management.
2) As suggested above, put the value in the id that Hibernate will understand as identifying a new instance. If the key is an object, it's often a null value. If the key is a native type, then it's often 0. See unsaved-value attribute on the <id> tag if it's been specified.

After that, any session.save() on the instance should trigger an insert at the session flush().

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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