-->
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.  [ 5 posts ] 
Author Message
 Post subject: Version number in optimistic locking scenario not updated
PostPosted: Wed Jul 28, 2004 5:19 am 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:02 am
Posts: 46
Location: Kuala Lumpur, Malaysia
Context: Hibernate 2.1.4, JBoss 3.2.3 environment

In my code below, I can successfully update an existing object and also create new
objects.

However my version column is not being updated and stays at zero.

My mapping for the version column:
Code:
<property name="version" column="version" type="long" >



The code:
Code:
Session session = HibernateSessionUtility.currentSession();
List list = session.find("from Foo as foo where foo.id = ?",
                                     key,
                                     Hibernate.STRING);
if (list.size() > 0) {
    logger.info("Replacing existing foo: " + key);               
    Foo persistentFoo = (Foo)list.get(0);               
    persistentFoo.setValue("updated Value");
               
}else if(list.size() == 0) {
    logger.info("Creating new foo: " + key);
    Foo transientFoo = new Foo();
    transientFoo.setValue("new Value");
    session.save(transientFoo);
}
session.flush();


Thanks.

Alistair


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 8:15 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
your version mapping is not correct, use <version column=.../>
instead of <property name="version" column=.../>
http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-version


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 10:12 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:02 am
Posts: 46
Location: Kuala Lumpur, Malaysia
Thank you. It's working now.

Regards,

Alistair


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 10:29 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:02 am
Posts: 46
Location: Kuala Lumpur, Malaysia
Could someone also confirm these thought of mine:

1) One should not add the version column to the equals/hashCode method because equals is used only for logical equality during code execution.

2) Whereas the version column is used by Hibernate or application developer to do a "dirty check" between the in-memory instance and its persisted instance.

Another quesiton: in the event of the version number difference, what will Hibernate do when I update a persistent object ?

Thanks

Alistair


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 29, 2004 3:12 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
alistair wrote:
1) One should not add the version column to the equals/hashCode method because equals is used only for logical equality during code execution.

http://www.hibernate.org/109.html
You are right, don't add version to the equals/hashCode. Authours of HIA (chapter 4.1.6) , argue to use "business key equality", that is implement equals/hashCode using only business fields that form business key/natural key (unique combination of business fields), and don't use any synthetic identifiers.

alistair wrote:
2) Whereas the version column is used by Hibernate or application developer to do a "dirty check" between the in-memory instance and its persisted instance.

Version is checked in the SQL UPDATE:
Code:
update User set name='new name' version=3 where userId=123 and version=2

if JDBC execute statement returned zero, you will get StaleObjectStateException. As you see performance do not suffer when you use versioned classes.
Delete should be handled similar. (where userId=123 and version=2)

[quote="alistair"]Another quesiton: in the event of the version number difference, what will Hibernate do when I update a persistent object ?[/code]
you will get StaleObjectStateException

--
Leonid


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