Hibernate Versioning Example Hibernate Versioning Of Objects

Once an object is saved in a database, we can modify that object any number of times right, If we want to know how many no of times that an object is modified then we need to apply this versioning concept.
When ever we use versioning then hibernate inserts version number as zero, when ever object is saved for the first time in the database.  Later hibernate increments that version no by one automatically when ever a modification is done on that particular object.
In order to use this versioning concept, we need the following two changes in our application

  • Add one property of type int in our pojo class
  • In hibernate mapping file, add an element called version soon after id element

Files required to execute this program..

  • Product.java (My POJO class)
  • Product.hbm.xml  (Xml mapping file )
  • hibernate.cfg.xml  (Xml configuration file)
  • ClientForSave_1.java (java file to write our hibernate logic)
  • ClientForUpdate_2.java

Product.java

12345678910111213141516171819202122232425262728293031323334353637383940414243444546package str;

public class Product{

	private int productId;
	private String proName;
	private double price;

private int v;

public void setProductId(int productId) { this.productId = productId; } public int getProductId() { return productId; } public void setProName(String proName) { this.proName = proName; } public String getProName() { return proName; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } public void setV(int v) { this.v = v; } public int getV() { return v; } }

Product.hbm.xml

123456789101112131415<?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="str.Product" table="products">

<id name="productId" column="pid"  />
<version name="v" column="ver" />
<property name="proName" column="pname" length="10"/>
<property name="price"/>

</class>
</hibernate-mapping>

Note:

  • In this above mapping file, find the <version> element, there i have given column name as version, actually you can write any name its not predefined.

 

hibernate.cfg.xml

1234567891011121314151617181920<?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">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<mapping resource="Product.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

ClientForSave_1.java

123456789101112131415161718192021222324252627282930package str;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class ClientForSave_1 { 

    public static void main(String[] args)
    {

        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml"); 

        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();
        Product p=new Product(); 

p.setProductId(104);

p.setProName("AC");

p.setPrice(14000);

Transaction tx = session.beginTransaction(); session.save(p); System.out.println("Object saved successfully.....!!"); tx.commit(); session.close(); factory.close(); } }

Note:

  • Remember friends, first we must run the logic to save the object then hibernate will inset 0 (Zero) by default in the version column of the database, its very important point in the interview point of view also
  • First save logic to let the hibernate to insert zero in the version column, then any number of update logic’s (programs) we run, hibernate will increments +1 to the previous value
  • But if we run the update logic for the first time, hibernate will not insert zero..! it will try to increment the previous value which is NULL in the database so we will get the exception.

 

ClientForUpdate_2.java

123456789101112131415161718192021222324252627282930package str;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class ClientForUpdate_2 { 

	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(Product.class,new Integer(104));
		Product s=(Product)o;

		Transaction tx = session.beginTransaction();	

s.setPrice(4000); // implicitly update method will be call

tx.commit(); System.out.println("Object Updated successfully.....!!"); session.close(); factory.close(); } }

First run the ClientForSave_1.java, then only ClientForUpdate_2.java
&

Note(Important..!!!)______
Guys your know some thing.., actually we can run any logic (Save or Update) ? for the first time, but make sure the versioning column is a number (>=0), but save logic has ability to insert zero by default if there is no value, and update logic will directly tries to increments already existing value by 1, it wont insert any value by default if its null, hope you are clear about this point, and mates its the very important concept in the interview point of view, if you have any doubt just Ask a Question on this our team will respond ASAP

Related Articles

post a comment