Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Automatic Versioning

I have developed a customer maintenance application. Users can alter the customer details via web interface. I want to handle the following scenario:

  1. User 1 loads customer1 details.
  2. User 2 loads customer1 details.
  3. User 1 changes and saves customer1's name.
  4. User 2 only changes and saves customer1's age.

In the scenario above, finally database holds customer1's old name and the new age because User 2 overwrites User 1's update. I'm using Hibernate. I had heard that Hibernate Automatic Versioning supports this. If any one know how to handle this please advise me.

like image 873
Niroshan Abayakoon Avatar asked Oct 03 '11 05:10

Niroshan Abayakoon


1 Answers

You just need to add a field annotated with @Version:

public class Customer {

 @Id
 private Long id;

 @Version
 private Long version;

 // rest of the fields, etc.

}

Read this article for more information.

like image 155
Behrang Avatar answered Oct 01 '22 17:10

Behrang