hibernate prevent concurrent update

Hibernate prevent concurrent update

Hibernate prevent concurrent update is an important concept in hibernate framework and Interviewer will ask you like how to prevent hibernate concurrent update.

Before moving towards the topic let’s understand what is concurrent update problem.

At a time more than one developer want to update same data in database using hibernate framework then we will get concurrent update problem and last updated value will not be reflected because in database.

To overcome above problem hibernate given version checking, version checking used in hibernate when more then one thread trying to access same data.

How to implement version checking in Hibernate

Make sure that at the time inserting any record in database using hibernate framework you have to use version field in Entity class as a new int or long or TimeStamp data type flag.

Hibernate has given <version name=”field_name”> tag to maintain version checking in database.

Whenever you will insert or update record then version value will be incremented by one and you no need to pass version value at the time inserting record.  Hibernate will insert int or long field as 0 first time and SQL TimeStamp as current system time stamp.

Hibernate prevent concurrent update Example

  1. Employee.java
  2. Employee.hbm.xml
  3. hibernate.cfg.xml


package com.javagf.bean;
public class Employee { 
 private int id; 
 private String name;
 private Integer salary;
 private int version;
 public Integer getSalary() {
    return salary;
 public void setSalary(Integer salary) {
     this.salary = salary;
 public int getId() {
     return id; 
 public void setId(int id) { 
    this.id = id;
 public String getName() {
     return name;
 public void setName(String name) {
     this.name = name; 
 public int getVersion() {
    return version;
 public void setVersion(int version) {
    this.version = version; 
 @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + ", version=" + version + "]"; }


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 18 Jun, 2017 2:16:54 PM by Hibernate Tools 3.5.0.Final -->
   <class name="com.javagf.bean.Employee" table="EMPLOYEE">
   <id name="id" type="int">
    <generator class="assigned" />
  <version name="version"></version> 
  <property name="name" type="java.lang.String"></property>
  <property name="salary" type="int"></property> 


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
   <property name="hbm2ddl.auto">update</property>
   <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
   <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> 
   <property name="connection.username">system</property>
   <property name="connection.password">manager</property>
   <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <mapping resource="com/javagf/bean/Employee.hbm.xml" />


package com.javagf.test;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.javagf.bean.Employee;

public class Run {
 public static void main(String[] args) { 
      Configuration cfg = new Configuration();
      SessionFactory factory = cfg.buildSessionFactory();
      Session session = factory.openSession();
      Query query = session.createQuery("from Employee where id=:id");
      query.setParameter("id", 101);
      Employee employee = (Employee) query.list().get(0); 
      int version = employee.getVersion();
      Transaction transaction = session.beginTransaction(); 
      session.createQuery("update versioned Employee set name=:name where version=:version") .setParameter("name", "Vicky").setParameter("version", version).executeUpdate();
      System.out.println("Record updated.");

Related Posts

  1. Why hibernate is better than JDBC
  2. Hibernate Architecture
  3. Steps to create hibernate application in eclipse
  4. Hibernate Mapping and Configuration file
  5. How to install Hibernate Tools in Eclipse
  6. Hibernate SQL Dialects List
  7. Hibernate Query Language
  8. Hibernate HQL select query example
  9. hibernate hql update query example
  10. hibernate hql delete query example
  11. Hibernate ID generator
  12. insert data into table using hibernate example
  13. update record database using hibernate
  14. Delete record database using hibernate
  15. hibernate select query example
  16. how to get all record from table in hibernate
  17. difference between wrapper and primitive types in hibernate
  18. Hibernate Criteria API
  19. Hibernate restrictions methods
  20. Hibernate Native SQL Query
  21. Hibernate Named Query
  22. Hibernate Caching – First Level Cache.
  23. Hibernate Second Level cache

Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229