@Entity public class Foo { // ... prive Long version; @Version public Long getVersion() { return version; } public void setVersion(Long version) { this.version = version; } // ... }
On each transaction commit if the entity is modified (persist or merge) the version column is increased by one.
Other possibilities
Instead of a Long you could use a Integer or a Date..but Date has performance problems and does not guarantee good behavior if two commits are happened with milliseconds time difference (see http://swinbrain.ict.swin.edu.au/wiki/Implementing_Concurrency_Controls_using_Hibernate).See also...
If you get StaleObjectStateException or OptimisticLockException then make sure that you don't persist the loading time of the entity (see http://micharg.blogspot.com/2012/02/jpaversioning-thrown.html)How to catch a OptimisticLockException
No comments:
Post a Comment