Hibernate Automatic Dirty Checking

date
Jan 27, 2024
slug
hibernate-dirty-checking
status
Published
tags
HIBERNATE
summary
type
Post

What is Dirty Checking?

Hibernate has the ability to automatically update the database when it detects changes made to the database entity within the transaction context. This feature of Hibernate is known as Hibernate automatic dirty checking. In essence, it saves us from having to ask Hibernate to update the database each time we change an object's state during a transaction.
NOTE Changes can only be detected within a transaction.

Example

User Class

Main

  1. First we fetch User with primary key 1 from the context.
  1. Second we change the value of balance.
Here hibernate automatically detects that value of User balance has been changed and automatically issues the UPDATE statement to the database before the commit.

LOGS

As we can see from the logs. An update statement has been executed by hibernate. This is hibernate dirty checking in action.
 
 

© Rupesh Dang 2021 - 2025