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. Comment More infoAdvertise with us Next Article Spring @Required Annotation with Example C chaitanyamunje Follow Improve Article Tags : Java Java-Hibernate Java-Spring-Data-JPA Practice Tags : Java Similar Reads Hibernate - @Transient Annotation with Example @Transient annotation in Hibernate is used to mark a property or field in an entity class as transient. This means that the field or property marked as transient should be excluded when the data persists in the database. Wherever an entity is mapped to a database table, by default all the non-transi 4 min read Hibernate - @OrderBy Annotation with Example @OrderBy annotation is used in Hibernate to specify the ordering of the elements in the collection valued property of an entity class. It is used to define the order of the elements which should be displayed. We can order the data within the entity using a specific parameter in ascending or descendi 4 min read Spring @Required Annotation with Example Spring Annotations provide a powerful way to configure dependencies and implement dependency injection in Java applications. These annotations act as metadata, offering additional information about the program. The @Required annotation in Spring is a method-level annotation used in the setter method 5 min read Spring @Qualifier Annotation with Example Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. Spring focuses on providing various ways to manage business objects, making web application development e 6 min read Spring @Value Annotation with Example The @Value annotation in Spring is one of the most important annotations. It is used to assign default values to variables and method arguments. It allows us to inject values from spring environment variables, system variables, and properties files. It also supports Spring Expression Language (SpEL) 6 min read Spring MVC @ModelAttribute Annotation with Example In Spring MVC, the @ModelAttribute annotation binds a method parameter or method return value to a named model attribute and then exposes it to a web view. It refers to the property of the Model object. For example, if we have a form with a form backing object that is called "Student" then we can ha 8 min read Like