Dirty Checking In Hibernate

Advertisement

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

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.

Example

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.