Hibernate Versioning Concept
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
- 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
Product.hbm.xml
- 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
<?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
- 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
&
Eclipse Output
In the database
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
No comments:
Post a Comment