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 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 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 Criteria Query Language (HCQL)

Hibernate Criteria Query Language (HCQL) is mainly used for searching and fetching records. It works on the filtration rules and logical conditions. The Criteria interface, Restriction class, and Order class are available in...

4 minutes read.

Hibernate Query Language (HQL)

Hibernate provides its query language called Hibernate Query Language (HQL). HQL is an object-oriented query language, similar to the native SQL language, and it works with persistent objects. It is a database-independent and...

2 minutes read.

Hibernate Session Interface

Hibernate Session Interface A Session is an interface between the Java application and the Hibernate. It is available in the org.hibernate.session package. It should not be kept open for a long time, as it...

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

Hibernate Annotation with 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 the specifications of the...

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

Hibernate merge() vs. update()

In Hibernate, both update() and merge() methods are used to convert the detached state object into the persistent state. session.merge() The merge() method is used when we want to change a detached entity into...

2 minutes read.

Hibernate Features

1. Lightweight Hibernate is a lightweight framework as it does not contains additional functionalities; it uses only those functionalities required for object-relational mapping. It is a lightweight framework because it uses persistent...

2 minutes read.

Hibernate Named Query using Annotation

In annotation-based named query, we use @NamedQuery annotation inside the POJO class. @NamedQuery- It is used to describe a single named query. It consists of four attributes- two of which are compulsory, and...

2 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 One-to-Many Mapping

One-to-Many Hibernate Mapping Example In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related...

3 minutes read.

Hibernate vs. JDBC

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

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

Hibernate Many-to-One Mapping

Many-to-One Hibernate Mapping with Example A Many-to-One association mapping is the reverse of One-to-Many association mapping. For example, many (customers) are associated with one (vendor). In Hibernate, Many-to-One association mapping is applied from child class...

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 GeneratedValue Strategies

Hibernate GeneratedValue Strategies Hibernate supports some generation strategies to generate a primary key in the database table. We can access the strategy with the help of @GeneratedValue annotation. @GeneratedValue The @GeneratedValue annotation specifies how to...

3 minutes read.