Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Versioning of An Object in Hibernate

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 607
    Comment on it

    As we know that the Hibernate API provide us to save, retrieve, update and delete data features, but it's not just that it provides more than that. In traditional database programming what we do to check the data is being modified, answer is that we need to create a extra column for the modify status and every time the data is changed we need to fire a update query to update the modify status.

    So we need to careful that we should update status every time the object is updated. But hibernate supports the versioning of an object. We just need to configure it once for the Model and each time the object will be updated hibernate take care of that versioning.

    When we use versioning using hibernate, the first time when the object will save in the database, hibernate inserts zero as a value in version number field. And after every time when the object will update, hibernate will increment that version number by one automatically.

    The following are the steps to use the versioning concept :

    1. First add a property of type int in the Pojo class.
    2. And secondly add the element <version> just after the <id> element in mapping file.

    Hibernate Versioning Example :

    package com.evon;
    
    public class Student{
    
        private int studentId;
        private String studentName;
        private int version;
    
        public void setStudentId (int studentId){
            this. studentId = studentId;
        }
        public int getStudentId (){
            return studentId;
        }
    
        public void setStudentName (String studentName){
            this. studentName = studentName;
        }
        public String getStudentName (){
            return studentName;
        }
    
        public void setVersion (int version){
            this. version = version;
        }
        public int getVersion (){
            return version;
        }
    }
    

    Student.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    <class name="com.evon.Student " table="student">
    
    <id name="studentId" column="studentId"  />
    <version name="version" column="version" />
    <property name="studentName" column="studentName" length="10"/>
    
    </class>
    </hibernate-mapping>
    

    hibernate.cfg.xml

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    <session-factory>
    <property name="connection.driver_class">com.mysql.jdbc.driver
    </property>
    <property name="connection.url">jdbc:mysql://localhost:3306/college</property>
    <property name="connection.username">admin</property>
    <property name="connection.password">password</property>
    
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    
    <mapping resource="Student.hbm.xml"></mapping>
    </session-factory>
    </hibernate-configuration>
    

    RegisterStudent.java

    package com.evon;
    
    import org.hibernate.*;
    import org.hibernate.cfg.*;
    
    public class RegisterStudent { 
    
        public static void main(String[] args){
    
            Configuration cfg = new Configuration();
            cfg.configure("hibernate.cfg.xml"); 
    
            SessionFactory factory = cfg.buildSessionFactory();
            Session session = factory.openSession();
            Student student=new Student(); 
    
            student.setStudentId (101);
            student.setStudentName("Gautam Prakash");
    
            Transaction tx = session.beginTransaction();
            session.save(student);
            System.out.println("Student saved successfully.....!!");
            tx.commit();
            session.close();
            factory.close();
        }
    }
    

    Note that when we run the above program the new student object is inserted to the database using the save method which fires the insert sql command internally and the value of the version column will be zero at the first time. So in db the value will be inserted as follows :

    studentId = 101,  studentName =  'Gautam Prakash' , and version = 0
    

    Now when we load this object and update something then after committing the db transaction the hibernate will increment +1 to the previous version column value. But if we run the update command first time when table is empty the hibernate will not insert zero and throw the exception because it try to search the previous value and try to increment +1on a value which is null at first time.

    StudentUpdate.java

    package com.evon;
    
    import org.hibernate.*;
    import org.hibernate.cfg.*;
    
    public class StudentUpdate { 
    
        public static void main(String[] args)
        {
    
            Configuration cfg = new Configuration();
            cfg.configure("hibernate.cfg.xml"); 
    
            SessionFactory factory = cfg.buildSessionFactory();
            Session session = factory.openSession();
            Object o=session.load(Student.class,new Integer(101));
            Student student=(Student)student;
    
            Transaction tx = session.beginTransaction();    
    
             student.setStudentName(Gautam Singh); // implicitly update method will be call
    
            tx.commit();
    
            System.out.println("Student Updated successfully.....!!");
            session.close();
            factory.close();
        }
    }
    

    Ass soon as we run the above program the value of the version column in db will increment by +1 and updated values are :

    studentId = 101,  studentName =  'Gautam Singh' , and version = 1
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: