Hibernate - @Version Annotation with Example Last Updated : 07 Jun, 2023 Comments Improve Suggest changes Like Article Like Report @Version annotation is used to specify the version number for a specific entity. Version Number provided using @Version annotation is used to prevent concurrent modification to an entity. When an entity is being updated, the version number is also incremented. If another transaction tries to update the same entity with the older version number, an exception will be given. The version number helps to prevent conflicts between concurrent transactions. @Version annotation is used for optimistic locking. Optimistic locking is a concurrency control mechanism in which it ensures that concurrent transactions will not conflict with each other. If there is a chance of conflict during transactions the transaction having an older version number will be aborted. Examples of @Version AnnotationExample 1: Java package com.example.java_test_application; // on the below line creating an entity // class for the class of Employee. @Entity public class Employee { // on the below line creating an employee id // generated value with the strategy for generation type. @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long empId; private String employeeName; private String employeeQualification; // on the below line we are // using version annotation. @Version private int version; } Explanation: In the above example, we are considering an Employee entity with a version field annotated with @Version. The version field will be used for optimistic locking. Whenever this entity field is being updated sequentially the value of the version is also checked to ensure that it matches the current version in the database. If the version has changed, an exception will be thrown, indicating a concurrent modification. Example 2: Java // on the below line creating an // entity class for the class of Department. @Entity public class Department { // on the below line creating a // variable for department id. @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private Long departmentID; // on the below line creating a // variable for department name. private String departmentName; // on the below line creating // a variable for version. @Version private Long version; } Explanation: In the above example, we are considering a Department entity in which we are creating a version field annotated with @Version. This version field is used for optimistic locking. When the Department entity is being updated it will first check the version to ensure that it will match the current version in the database. If the version has changed an exception will be thrown indicating a concurrent modification. Create Quiz Comment C chaitanyamunje Follow 0 Improve C chaitanyamunje Follow 0 Improve Article Tags : Java Java-Hibernate Java-Spring-Data-JPA Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like