Will hibernate update the database if a persistence instance has been installed but not modified?
If I have a persistence object instance and I set some items like this:
MyThing thing = session.get(MyThing.class, id);
thing.setSomething(thing.getSomething());
session.update(thing);
Would this be the reason hibernate issues a SQL update command to update the record? Or is hibernation smart enough to know that the object has been updated, but the values ββhaven't actually changed?
a source to share
In general, it should not issue an update if the property values ββcompare equal. (In particular, compare equals according to their type implementation - this is usually the same as their equals (Object) method).
nemo was right, you should always know what SQL Hibernate is doing. Although I recommend setting the logger org.hibernate.SQL ('net.sf.hibernate.SQL' for Hibernate 2) to DEBUG level (in log4j) rather than using the show_sql property (which is always written to System.out or System.err iirc ).
(Although I think you meant session.flush()
, not session.update(thing)
... unless you are using a stateless session?)
a source to share
I believe it will be smart enough to recognize that the object has not been modified, thus avoiding the update.
Of course, you need to enable SQL inference for Hibernate. Add to
<property name="hibernate.show_sql">true</property>
To the configuration section of the hibernate session factory in hibernate.cfg.xml.
a source to share