Dirty Checking in Hibernate

Dirty checking is an essential concept of Hibernate. The Dirty checking concept is used to keep track of the objects. It automatically detects whether an object is modified (or not) or wants to be updated.

It also allows a developer to avoid time-consuming database write actions. It modifies only those fields which require modifications, and the remaining fields are kept unchanged.

Example of Dirty Checking

Let’s understand the concept of dirty checking with the help of an example. In this example, we are taking an entity class Student. The Student class contains student id (id), name (Sname), course (Scourse), and rollno (Srno) of the student. It also provides default and a parameterized constructor.

To access dirty checking in the application, we are using the annotation @DynamicUpdate to the entity class Student.

@DynamicUpdate- It is used for updating the objects. This annotation makes the necessary modifications and changes to the required fields.  

Following code is of the Student class.

Student.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.DynamicUpdate;
@Entity
@Table(name="dirtycheck")
@DynamicUpdate
public class Student {
            @Id
             @GeneratedValue(strategy=GenerationType.AUTO)
             @Column(name="S_id")
             private int id;
             @Column(name="S_name")
             private String Sname;
             @Column(name="S_course")
             private String Scourse;
             @Column(name="S_rno")
             private int Srno;
             public int getId() {
                         return id;
             }
             public void setId(int id) {
                         this.id = id;
             }
             public String getSname() {
                         return Sname;
             }
             public void setSname(String sname) {
                         Sname = sname;
             }
             public String getScourse() {
                         return Scourse;
             }
             public void setScourse(String scourse) {
                         Scourse = scourse;
             }
             public int getSrno() {
                         return Srno;
             }
             public void setSrno(int srno) {
                         Srno = srno;
             }
             public Student(){
             }
             public Student(int id, String sname, String scourse, int srno) {
                         super();
                         this.id = id;
                         Sname = sname;
                         Scourse = scourse;
                         Srno = srno;
             }
    } 

Now, we are going to create the configuration file. It contains information about the database and the mapping class.

hibernate.cfg.xml


org.hibernate.dialect.MySQL5Dialect
com.mysql.jdbc.Driverjdbc:mysql://localhost:3306/test3
root
root
update
truetrue
 

 

Now, we are going to create the main class which stores the object of the entity class.

App.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class App 
 {
     public static void main( String[] args )
     {
        Configuration cfg=new Configuration();
        cfg.configure("hibernate.cfg.xml");
        SessionFactory fac= cfg.buildSessionFactory();
        Session ses= fac.openSession();
        ses.beginTransaction();
       //setting the values of Student class variables
       /* Student s= new Student();
        s.setScourse("bca");
        s.setSname("jyotika");
        s.setSrno(14);
        ses.save(s);
        ses.getTransaction().commit();
        ses.close();
        */
       //updating the existing record
        Student st= ses.get(Student.class, 1);
        st.setScourse("bba");
        ses.update(st);
        ses.getTransaction().commit();
        ses.close();
        System.out.println("updated");
     }
 } 

OUTPUT – After Insertion

Following output occurs when the data is inserted in the dirtycheck table.

Database Table – dirtycheck

OUTPUT- After Updation

Following output occurs when the data is updated in the dirtycheck table.

Database Table- dirtycheck

We have updated the S_course in the database table. The course is updated from “bca” to “bba.