Web design tools - CHAPTER 10 Transactions and concurrency Versioning with Java

CHAPTER 10 Transactions and concurrency Versioning with Java Persistence The Java Persistence specification assumes that concurrent data access is handled optimistically, with versioning. To enable automatic versioning for a particular entity, you need to add a version property or field: @Entity public class Item { … @Version @Column(name = “OBJ_VERSION”) private int version; … } Again, you can expose a getter method but can t allow modification of a version value by the application. In Hibernate, a version property of an entity can be of any numeric type, including primitives, or a Date or Calendar type. The JPA specification considers only int, Integer, short, Short, long, Long, and java.sql. Timestamp as portable version types. Because the JPA standard doesn t cover optimistic versioning without a version attribute, a Hibernate extension is needed to enable versioning by comparing the old and new state: @Entity @org.hibernate.annotations.Entity( optimisticLock = org.hibernate.annotations.OptimisticLockType.ALL ) public class Item { … } You can also switch to OptimisticLockType.DIRTY if you only wish to compare modified properties during version checking. You then also need to set the dynamicUpdate attribute to true. Java Persistence doesn t standardize which entity instance modifications should trigger an increment of the version. If you use Hibernate as a JPA provider, the defaults are the same every value-typed property modification, including additions and removals of collection elements, triggers a version increment. At the time of writing, no Hibernate annotation for disabling of version increments on particular properties and collections is available, but a feature request for @OptimisticLock(excluded=true) exists. Your version of Hibernate Annotations probably includes this option. Hibernate EntityManager, like any other Java Persistence provider, throws a javax.persistence.OptimisticLockException when a conflicting version is
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Leave a Reply