Versioning In Hibernate

Posted By : Rahul Jha | 29-Jun-2015


This keeps track of the number of times each record has been modified by using Hibernate persistance logic. This help us to enable to enable payments on customer details modification in service sectors/ banking application / telephone network provider and etc. Procedure to enabling versioning on the Db table records through hibernate Step 1: Keep one hibernate application ready where we talk about employee

Step 2: Add the following column in employeee table by having initial value 0

  sql> alter table employee add ver_col number(5);
  sql> update employee set ver_col=0;
  sql> commit;

Step 3 : Add special property in employee pojo class to keep track of this versioning.

In Employee Pojo Class


 public class Employee{
  int ver; // special property for versioning
  public void setVer(int ver)
   this.ver = ver;
  public int getVer()
   return ver;

step 4: Configure the above special property in hibernate mapping file using 'version' tag In mapping file:

// This tag must be write after id tag and before property tag

   <version column="ver_col" name="ver">

Step 5: Pseudo code for logic to access the record and modify the record In client class ==============

   // select a record 
   EmpBean eb = (Employee) ses.load(EmpBean.class,1010) // 1010 is the id of employee assume it

   eb.setMail('[email protected]')
   Transaction tx = ses.beginTransaction();
   System.out.println("Number of times that this record is modified"+eb.getVer());



About Author

Author Image
Rahul Jha

Rahul is an expert Java developer having experience in frameworks like spring and hibernate.

Request for Proposal

Name is required

Comment is required

Sending message..