tags:

views:

344

answers:

2

Hello Experts,

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

For Example having 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 (thats my "impression" of hibernate) that when working with a standard Hibernate mapping (mapping the full class), even updates of only one single member lead to a full update of the object in SQL Statements generated by Hibernate.

My Questions are:

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

2.) 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 some big work on doing dirty-checking, but as far as I know this dirtychecking is only relevant to identify if the object as whole is dirty, not what single member is dirty.)

Thank you very much! Jens

A: 

Actually you can specify the options dynamic-update and dynamic-insert in a class mapping. It does just that. More info here.

Maurice Perry
Hello Maurice, thank you very much!!! Your help was absolute helpfull. I was not aware of the "dynamic-update" feature and with your help I could google for it and now I absolutely know how to solve my problem. Thanks very very much!!!
jens
A: 

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);

person.setName("jean");

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

UPDATE PERSON (NAME, ADDRESS, DATE) VALUES (?, ?, ?);

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

Arthur Ronald F D Garcia
Hello Arthur, thank you too for your answer!! The "dynamic-update" Keyword is really what solves my problem. Thanks !
jens
@jens Are you sure you want to use dynamic-query ??? Each Time you *dynamic-update* your instance, Hibernate needs to create a new update query *instead of use a cached update query*. Be aware of performance issues.
Arthur Ronald F D Garcia
thank for your reply. You are of course right. Normally I will not use dynamic-upate. But I have a very special requirement in an "Multi Concurrent" Environment where several Processes will update the Record/Objet. But to my luck the upates are not related. Process A will always upate Columns 1,2 and Process B will update Columns 3,4 etc... By using "dynamic-updates" I can only update 1,2 OR 3,4 without any problems. However when I would update the whole object with all Columns 1,2,3,4 I would have to introduce Versioning and handle the failed updates... In general you are absolutely right.
jens