tags:

views:

141

answers:

3

If I have an instance of a persistence object, and I set some members in like so:

MyThing thing = session.get(MyThing.class, id);
thing.setSomething(thing.getSomething());
session.update(thing);

Will this actually cause hibernate to issue an SQL update command to refresh the record? Or is hibernate smart enough to know that the object was updated, but the values have not actually changed?

+1  A: 

I believe it is going to be smart enough to recognize that the object has not been changed, thus avoiding the update.

The way to tell for sure is to turn on SQL output for Hibernate. Add

<property name="hibernate.show_sql">true</property>

To the session-factory section of hibernate-configuration, in your hibernate.cfg.xml.

James McMahon
A: 

AFAIK, it will not execute an SQL command.

Frederik Gheysels
+2  A: 

In general, it should not issue an update if the property values compare equal. (Specifically, compare equal according to their Type implementation--- which is usually the same as their equals(Object) method).

nemo was right, you should always be aware of what SQL Hibernate is executing. Although I recommend setting the 'org.hibernate.SQL' logger ('net.sf.hibernate.SQL' for Hibernate 2) to DEBUG level (in log4j) rather than using the "show_sql" property (which always writes to System.out or System.err iirc).

(Although I think you meant session.flush(), not session.update(thing) ... unless you're using a stateless session?)

araqnid
Excellent point about the equals compare.
James McMahon