@Version Annotation Example In Hibernate

In this article, we will see @Version Annotation Example In Hibernate Using Spring Boot. We will see the complete example from scratch but before going ahead let’s see below points which demonstrate how @Version behaves in a different scenario.

@Version Annotation Example In Hibernate

Hibernate version default value – When we create an entity for the first time default version value is zero(the field which is annotated with @Version), which hibernate takes care.

When does hibernate version increase – When we modify some entity’s field and update that entity the version will increase. As many time we will update the entity version will increase, but at least one field should be changed. If we don’t modify any field and try to update the entity, the version will not update.

What will happen if don’t provide correct version – Suppose we are trying to update an entity and we are not providing the correct version(for example in database version value is 1 and we are providing version value 0 or 2 any number rather than 1) we will get an exception.

“optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction.”

Note – If we don’t provide the latest version it will throw an exception and we will not able to perform the update operation.

How to use @Version annotation with the field – See below sample code.


When we create an entity for the first time default version value will be zero(we will see in the example later). Now we want to update this entity(modifying some field) we need to pass the same version number and our version should increase by +1. For example, consider we have an entity called Student which has four field id, studentName, rollNumber and version. If we modified one or more fields(i.e studentName or rollNumber ) of the entity and try to update this entity, the version will increase by +1. The @Version annotation in hibernate is used for Optimistic locking while performing update operation. In the Optimistic Locking approach first, we read a record from the database(this record contains the version number) and the user updates this record which we have just read from the database(suppose the user has modified one field in entity and try to update that entity). In this case, User will only able to update the record if the version hasn’t been changed by another user before you write the record back into the database.

To see how version works( in case of save and update) we will define two REST API as below.

http://localhost:9091/student/save – default version 0.

http://localhost:9091/student/update/1 – version will increase by 1. At least one field must modify else version will remain the same.

Let’s see @Version Annotation Example In Hibernate Using Spring Boot, Eclipse and Oracle from scratch.

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.  Fill all details(GroupId – versionexample, ArtifactId – versionexample and name – versionexample) and click on finish. Keep packaging as the jar.

Modify pom.xml

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

If you see any error for oracle dependency then follow these steps.

Directory structure –

@Version Annotation Example In Hibernate





StudentRepository.java – interface

StudentService.java – interface







Let’s run the SpringMain class(run as java application).

Perform save operation first using below REST API.


Request Data –

Response Data –

In response, we have version value is zero. When we create entity first time the default value of the version is zero. Now we will update the entity. Just we will change the name or rollNumber and we will see what does happen.

For update the entity we will use below REST API. Let’s update the rollNumber of the student.


Request Data –

Response Data –

Now version has been increased by +1. This is how @Version works in Hibernate. As many times we will update the student’s name or rollNumber version will increase by 1.

Now, what will happen if we want to update student entity but we don’t provide proper version? For example we want to update name too. Here what would be the version in request data? It would be 1 or 0?  When we are going to update entity for next time version should be current version i.e nothing but 1.

Valid Request Data –

Again the question comes what will happen if we try to update student entity with old version i.e 0(update rollNumber to 0126CS03)

Request data –


Response –

Oops ! We have exception



Note – When we create a new entity default version assign to zero by hibernate as below.



That’s all about @Version Annotation Example In Hibernate Using Spring Boot.

You may like.

See more about @Version. Here is the docs.