hibernate dirty checking and only update of dirty attributes

hibernate dirty checking and only update of dirty attributes  using -'java,hibernate'

In the "good old JDBC days" I wrote a lot of SQL code that did very targeted updates of only the "attributes/members" that were actually changed:

For example, consider an object with the following members:

public String name;
public String address;
public Date date;

If only date was changed in some Business Method I would only issue an SQL UPDATE for the date member.

It seems however (that's my "impression" of Hibernate) that when working with a standard Hibernate mapping (mapping the full class), even updates of only a single member lead to a full update of the object in the SQL statements generated by Hibernate.

My questions are:

Is this observation correct, that Hibernate does not intelligently check (in a fully mapped class), what member(s) where changed and then only issue updates for the specific changed members, but rather always will update (in the generated SQL Update Statement) all mapped members (of a class), even if they were not changed (in case the object is dirty due to one member being dirty...)
What can I do to make Hibernate only update those members, that have been changed? I am searching for a solution to have Hibernate only update the member that actually changed.

(I know Hibernate does quite some work on dirty-checking, but as far as I know this dirty checking is only relevant to identify if the object as whole is dirty, not what single member is dirty.)

asked Oct 7, 2015 by nikhilapatil
0 votes

3 Answers

0 votes

Hibernate just update what you really want to

public class Person {

    private Integer id;

    public String name;
    public String address;
    public Date date;

    // getter's and setter's


And you do something like

Person person = (Person) sessionFactory.openSession().get(Person.class, personId);


Hibernate is smart enough to know just name property has been changed. Although you can see your log as follows


Because Hibernate cache each SQL (INSERT, UPDATE, SELECT) query for EACH entity, again, it just update what you really want to.

answered Oct 7, 2015 by tushar2k6
0 votes

I think dynamic-update is good if you have heavy database load and one or multiple indexes to recreate on a full update (where all columns are updated, also those with unchanged values).

Maybe some DBMS recognize if UPDATE sets an already existing value to not update an index including that column. But many seem to be too "stupid" to recognize this (or don't check that for performance reasons).

Client side load for creating SQL queries is not a problem for most DB client applications. Interpretation of SQL in DB should take less time than recreating a big index.

Please correct me if I'm wrong!

answered Oct 7, 2015 by bhavin
0 votes

You can optimize dirty checking a lot by implementing interfaces like CustomEntityDirtinessStrategy or Interceptor. See working example at http://prismoskills.appspot.com/lessons/Hibernate/Chapter_20_-_Dirty_checking.jsp

answered Oct 7, 2015 by vimaldas2005