-->
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: Version incremented on each save?
PostPosted: Thu Apr 10, 2008 5:27 pm 
Newbie

Joined: Thu Apr 10, 2008 5:15 pm
Posts: 1
To learn how to use optimistic locking, I tried to create a very simple example program.

I have a simple Person entity, with a primary key, two properties firstName and name, and a version field (of type int).

Creating a Person and saving it gives it a version of 0. Now, if I just do

Code:
t=session.beginTransaction();
session.update(person);
t.commit();

the version field in my Person is incremented, even if the Person instance was not changed (not dirty). My expectation was that the version fields was only incremented when a "dirty" person was updated.

Is incrementing the version number on each update the expected behaviour, or did I miss something (maybe some configuration issue)?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 9:18 pm 
Beginner
Beginner

Joined: Fri Aug 24, 2007 4:46 am
Posts: 36
Location: Bielefeld / Germany
You can check if an object is dirty before updating:

Code:
t=session.beginTransaction();
...
if(session.isDirty(person)) {
   session.update(person);
}
...
t.commit();


HTH, Maik


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 2:57 am 
Regular
Regular

Joined: Mon Aug 06, 2007 10:49 am
Posts: 67
Location: Banska Bystrica, Slovakia
method session.update() will always execute sql update to database , no matter if object is dirty or no. it also attach object on session, if it was in detached state before.
So this method is good to persist changes on detached objects, because it always update object in db and also in ur case increment version.


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.