Dirty Checking In Hibernate

This section describes about the hibernate feature i.e. Dirty checking.



Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial

Dirty Checking In Hibernate

In this section we will read about the dirty checking in hibernate.

Dirty Checking is the feature of hibernate that helps in to automatically detect the modification of object in a transaction and update, if modified, the database. Inspection is a strategy that the hibernate uses for dirty checking. In an inspection strategy, a snapshot of the object at the time which it is loaded from the database is stored into the memory. In case of the session is flushed out, the stored object and the current state of object is compared by the Hibernate and if there is difference found in objects then it marks that object to dirty. A persistent state of object in hibernate specifies its bound for a particular hibernate Session and this object is in under surveillance of Hibernate. If any changes to the objects is found hibernate executes the SQL update statement.


Here I am giving a simple example which will demonstrate you about the dirty checking in hibernate. In this example you will see how the hibernate executes the update statement SQL. Here we will create a simple application into which we will show you how a update statement is called by the hibernate and the field in the database is updated. Here I have created a package in Java net.roseindia then I have created a JavaBean into which I have defined some properties like id and name and their setter getter method. Then I have created a class named PersonDetail.java into which I have generate a condition for generating the dirty checking. Before, creating the Java project we need to create a table named with person1 (Id, Name) and then inserted some dummy records into the table.

Create Table named person1

CREATE TABLE `person1` ( 
`Id` int(10) NOT NULL, 
`Name` varchar(15) default NULL, 

Insert some record into the table

insert into person1(Id, Name) values(1, 'Deepak')

Then the table will contain the value as below :

Now, Create the Java Application

Directory Structure


package net.roseindia;

public class Person
int id;
String name;
public Person()
public Person(int id, String name) {
this.id = id;
this.name = name;
public int getId() {
return id;
public void setId(int id) {
this.id = id;
public String getName() {
return name;
public void setName(String name) {
this.name = name;


package net.roseindia;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class PersonDetail
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static void main(String[] args)
Session session = null;
Configuration cfg = new Configuration().addResource(
serviceRegistry = new ServiceRegistryBuilder().applySettings(
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
catch (Throwable ex)
System.err.println("Failed to create sessionFactory object."+ ex);
throw new ExceptionInInitializerError(ex);
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

//loading a person object of id = 1
Person person = (Person)session.get(Person.class, new Integer(1));
//print the person name
//modify the person name

} catch (Exception e) {
} finally {


<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<hibernate-mapping package="net.roseindia">
<class name="Person" table="person1">
<id name="id" type="int" column="Id" >
<generator class="assigned"/>

<property name="name">
<column name="Name" />



<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/record
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>



When you will execute the PersonDetail.java class you will get the output at console as follows :

Data in the table after update

Download Source Code


Share on Google+Share on Google+

Dirty Checking In Hibernate

Posted on: July 10, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Dirty Checking In Hibernate  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image