This week I was confronted with a challenge to figure out when Hibernate does update a record and when not. The problem was that we had to know when Hibernate does update a record because of a version flag we are going to use for optimistic locking. The import thing was that a master record is not updated when a child record is updated and that a record is not updated when its content was just reset.
Most of the results where quite good and helped us a lot to keep changes at bay. Hibernate does not update a record even when its content is reset even when a string value is set of a field that has a different address as long as the content is equal. Also when a child is updated and its saved through its master record only the changed children records are updated. This enables two users to change different child records and safe them without any exceptions. Because we are using a Hibernate Interceptor to set an updated date field the field is not set and therefore the record isn't updated when the content has not changed. Using the Hibernate Interceptor really makes life really easy here.
Eventually I found scenario where it did not work out as expected. In as One-to-Many (and I also think in a Many-to-Many) relationship adding or removing a child makes Hibernate updating the master record even though on the DB nothing has changed for the Master. Removing the child directly from the DB and then re-fetching the master record does yield the same result but without the update of the master record. I don't know why the master record is updated and I think Hibernate should be able to detect this and don't do an update. It could be that there is a way to prevent that but for now I don't know how.
Cheers - Andy