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.”


Related Topics

Hibernate Merge

Hibernate Merge Merge is a method available in Hibernate which is used to update the existing records. However, it creates a copy from the passed entity objects and returns it. In other words, the merge()...

2 minutes read.

Hibernate vs. JPA

What is JPA? JPA stands for Java Persistence API, which defines a set of functionalities, standards, and concepts to the ORM tool. It is a framework used to manage relational databases. It is available...

2 minutes read.

Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

4 minutes read.

Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping...

3 minutes read.

Cascade in Hibernate

Cascading is a feature in Hibernate, which is used to manage the state of the mapped entity whenever the state of its relationship owner (superclass) affected. When the relationship owner (superclass) is saved/...

3 minutes read.

Hibernate Inheritance Table Per Class

In this inheritance strategy, table per class is generated. It means a separate table is generated for each POJO class involved in the hierarchy. Unlike the SINGLE_TABLE strategy, there are no nullable values...

3 minutes read.

Hibernate First-Level Cache

The first-level cache is related to the Session object. First level cache objects of one Session are not visible to the other Sessions, and all the cache data is lost when the Session...

2 minutes read.

Hibernate Web Application Example

Web Application Example with Hibernate In this section, we create a hibernate web Application. Here we are using a JSP file for presentation. Example of creating a web application using hibernate index.jsp This page will show a...

2 minutes read.

Architecture of Hibernate

Next → Hibernate is an ORM framework with a layered architecture. It consists of many objects, such as a persistent object, sessionfactory object, transaction object, session, etc.  There are mainly three layers in Hibernate...

2 minutes read.

Hibernate Dialects

Hibernate Dialects Dialect is a Java class available in org.hibernate.dialect package, which helps to map Java Application with the database. To interact with the database, we need to define the required database dialect in...

2 minutes read.

Hibernate first Example

Now we are going to create the first example in Hibernate. We will proceed step by step to develop the first Java application in Hibernate. The steps are listed below:- Create the POJO...

2 minutes read.

Hibernate Composite Primary Key

Hibernate Composite Primary Key A Composite Primary Key is a combination of one or more columns that forms a primary key. When a database table contains more than one primary key column, it is...

8 minutes read.

Annotation Example in Hibernate

Hibernate Annotation Example Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases, and it also uses...

3 minutes read.

Hibernate SessionFactory

Next → Hibernate SessionFactory A SessionFactory is an interface in Hibernate that create the instances of Session interface. It is a heavy weight object, and it is usually created during the application...

3 minutes read.

Hibernate Lifecycle

Hibernate Lifecycle In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do...

3 minutes read.

Hibernate Inverse

Hibernate Inverse An Inverse attribute is used to maintain the relationship between the parent and child class object. The inverse attribute is used only with bi-directional mappings such as one-to-many and many-to-many Hibernate mapping. In Hibernate, the...

2 minutes read.

Hibernate History and Versions

Next → History of Hibernate Hibernate was developed in 2001 by Gavin king with his colleagues from Circus Technologies. The main aim was to provide better persistence capabilities than those of EJB2, by reducing the...

2 minutes read.

Hibernate One-to-One Mapping Example

One-to-One Hibernate Mapping In One-to-One association mapping, one object of a persistent class is related to one object of another persistent class. Here, we are going to create an example of one-to-one mapping using...

3 minutes read.

Hibernate Tutorial

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

3 minutes read.

Hibernate Named Query Using XML

A named query is a technique used to assemble all the queries (Native SQL and HQL) in a specific location and refer them by some name. It helps in reducing the mess...

3 minutes read.