Hibernate automatic versioning

I developed a customer service application. Users can change customer data through the web interface. I want to process the following script:

  • User 1 loads parts customer1 .
  • User 2 loads customer1 parts.
  • User 1 modifies and saves customer1 name .
  • User 2 changes and saves customer1 age.

In the above scenario, the database ultimately contains the old name customer1 and the new age, as User 2 overwrites the update of User 1 . I am using hibernate. I heard that Hibernate Automatic Versioning supports this. If anyone knows how to handle this, please let me know.

+7
source share
2 answers

You just need to add the 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 details.

+12
source

one solution, when the second query tends to update the data, first check if it is updated after loading the data, if so, then throw an exception and allow the user to change after the data is downloaded again, you can use the modification timestamp to compare

0
source

All Articles