-->
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.  [ 2 posts ] 
Author Message
 Post subject: Configure optimistic locking using annotations
PostPosted: Thu Dec 02, 2010 11:58 am 
Newbie

Joined: Tue Oct 13, 2009 2:21 pm
Posts: 2
I would like to be able to enable optimistic locking using a version column that contains a timestamp. Since we're using a legacy database that cannot be changed simply, we prefer using the existing timestamp column instead of introducing a new version index column. The application can be deployed on multiple instances on which the system time is not necessarily synchronized, so we need to fetch the current timestamp from the database. I'm using hibernate with annotations. My domain classes now simply contain:
Code:
@Version
@Column(name = "last_modified")
private Date lastModificationDate;

This obviously works, but it uses the local system timestamp instead of fetching the database time. In a mapping file you can use source="db" as follows:
Code:
<timestamp name="lastModificationDate" column="last_modified" source="db"/>

How can I configure the same thing using annotations?


Top
 Profile  
 
 Post subject: Re: Configure optimistic locking using annotations
PostPosted: Fri Dec 03, 2010 4:51 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
From the documentation at http://docs.jboss.org/hibernate/core/3. ... ml#d0e5785

Quote:
When using timestamp versioning you can tell Hibernate where to retrieve the timestamp value from - database or JVM - by optionally adding the @org.hibernate.annotations.Source annotation to the property. Possible values for the value attribute of the annotation are org.hibernate.annotations.SourceType.VM and org.hibernate.annotations.SourceType.DB. The default is SourceType.DB which is also used in case there is no @Source annotation at all.


So I guess you shouldn't have to do anything (unless there is a bug in Hibernate or in the documentation). The strange thing is that if you are using a mapping file the default is source="vm".


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