-->
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: Object Versioning Best Practice
PostPosted: Fri Mar 03, 2006 10:09 am 
Newbie

Joined: Mon Feb 06, 2006 5:24 pm
Posts: 4
Is there a best practice for storing versions of objects?

I have a user configuration object that I want to be able to persist. There will be one active object, but many previous ones reflecting the state of their configuration at that point in time. When the user makes updates to their configuration, I want to make a new version of the object in the database. The obvious way to me is to make a deep copy and then use the usual save semantics. However, I was wondering if there is a way to detach the object and then save it so that it will get a new id and the old version will still be in the database and I avoid the deep copy?

Thanks,
Grant

_________________
------------------------------------
Grant Ingersoll
http://www.grantingersoll.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 03, 2006 10:13 am 
Beginner
Beginner

Joined: Wed Aug 04, 2004 4:33 am
Posts: 45
Location: Switzerland
I would say that if you detach it, set the primary key to null and then save it, hibernate would insert a new entry, since it thinks it is a new instance because the primary key is null.

_________________
------------------------------
please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 03, 2006 10:45 am 
Newbie

Joined: Mon Feb 06, 2006 5:24 pm
Posts: 4
szehnder wrote:
I would say that if you detach it, set the primary key to null and then save it, hibernate would insert a new entry, since it thinks it is a new instance because the primary key is null.


Here's what I did and it seems to work:

...
if (sess.contains(userConfiguration))
{
sess.evict(userConfiguration);
userConfiguration.setId(new Long(-1));//the unsaved-value is -1
}
result = (Long) sess.save(userConfiguration);

_________________
------------------------------------
Grant Ingersoll
http://www.grantingersoll.com


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.