Entity Versioning in AppFuse

Posted by Martin Homik | Posted in Java, WebApp | Posted on 26-03-2008

0

It’s time to write a few words about some things I have learned in the past week. The first thing I want to mention is versioning. So far, whenever I updated and persisted objects to the database, I calculated the modified time myself which is not that bad. But persistence layers such as Hibernate actually can do that for you. To prevent two users from writing to the database at the same time, locking techniques are provided. One such technique is called optimistic locking which is implemented by using versioning either with integers or with timestamps. If you select timestamps, the only thing you have to do is to specify which of the entity fields is supposed to be in charge of the version. Add a @Version JPA annotation to the field. Any persistence framework that is compliant to JPA will calculate the modified data for you and persist it to the database.

Find more explanations here and here.For Hibernate users the Java Persistence with Hibernate Book is the best choice.

The drawback of this approach is that once you edit an object, Hibernate creates a new revised object instead of altering the actual one. That’s a pity and does not serve my purpose in a current project. I am confused, because the book Hibernate in Action describes the versioning process differently: alterations are updates and no inserts. I am not able to resolve this issue.

Write a comment