Hibernate Tutorial

Hibernate Version Annotation Tutorial and Example

Hibernate Version management

The @version element is optional and indicates that the table contains versioned data. This is particularly useful if you plan to use long transactions, Version tag may help you to avoid concurrent updation for a row.

@Version
 @Column(name="hit")
 private int count;

Pojo

package com.candidjava.hibernate;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;
@Entity
@Table(name = "UsersTest1")
public class Student implements Serializable {
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue
  @Column(name = "STUDENT_ID")
  private long id;
  @Column(name = "Name", nullable = false, length = 100)
  private String name;
  @Column(name = "Degree", nullable = false, length = 10)
  private String degree;
  @Column(name = "Mobile", nullable = false, length = 100)
  private String phone;
  @Column(name = "Email", nullable = false, length = 100)
  private String email;
  @Version
  @Column(name = "hit")
  private int count;
  public int getCount() {
    return count;
  }
  public void setCount(int count) {
    this.count = count;
  }
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  public long getId() {
    return id;
  }
  public String getName() {
    return name;
  }
  public String getDegree() {
    return degree;
  }
  public String getPhone() {
    return phone;
  }
  public void setId(long string) {
    id = string;
  }
  public void setName(String string) {
    name = string;
  }
  public void setDegree(String string) {
    degree = string;
  }
  public void setPhone(String string) {
    phone = string;
  }
  public String toString() {
    return name;
  }
}

Updating versioned record

public void updateStd() {
    try {
      Session s = getSession();
      Transaction tx = s.beginTransaction();
      Student user = (Student) s.get(Student.class, student.getId());
      user.setName(student.getName());
      user.setEmail(student.getEmail());
      user.setDegree(student.getDegree());
      user.setPhone(student.getPhone());
      Thread.sleep(1000); // delay the update to do concurrent test
      s.update(user);
      tx.commit();
    } catch (HibernateException | InterruptedException e) {
      System.out.println(e.getMessage());
      System.out.println("updation failed");
    }
  }

Test Concurrency

Student sa=new Student();
    sa.setId(1);
    sa.setName("raman");
    sa.setEmail("raman@mail.com");
    sa.setPhone("9557857");
    sa.setDegree("bca");
 
    Student sas=new Student();
    sas.setId(1);
    sas.setName("ramaaaan");
    sas.setEmail("ramaaan@mail.com");
    sas.setPhone("953358775");
    sas.setDegree("Mca");
 
    StudentDao ms=new StudentDao();
    ms.setStudent(sa);
    
    StudentDao mss=new StudentDao();
    mss.setStudent(sas);
    
    ms.start();
    mss.start();

Download

Hibernate Version annotation example war

Hibernate version annotation example zip