pksiv wrote:
Hibernate will NOT issue an update if the values haven't changed.
You are
wrong if I understand the initial question correctly. We say about
"a field in an object", in other words about updating one separate field of the object which is mapped to some field(s) of database table(s).
Of course first of all we call
Code:
public boolean isDirty(Object old, Object current, SessionImplementor session)
for each field of obejct in order to understand we need to update the obejct in a whole or not. Then if some field (inlucding collections, components) was changed we perform the following method:
Code:
/**
* Update an object
*/
public void update(final Serializable id,
final Object[] fields,
final int[] dirtyFields,
final boolean hasDirtyCollection,
final Object[] oldFields,
final Object oldVersion,
final Object object,
final Object rowId,
final SessionImplementor session)
throws HibernateException {
//note: dirtyFields==null means we had no snapshot, and we couldn't get one using select-before-update
// oldFields==null just means we had no snapshot to begin with (we might have used select-before-update to get the dirtyFields)
final boolean[] tableUpdateNeeded = getTableUpdateNeeded( dirtyFields, hasDirtyCollection );
final int span = getTableSpan();
final boolean[] propsToUpdate;
final String[] updateStrings;
if ( entityMetamodel.isDynamicUpdate() && dirtyFields != null ) {
// For the case of dynamic-update="true", we need to generate the UPDATE SQL
propsToUpdate = getPropertiesToUpdate( dirtyFields, hasDirtyCollection );
// don't need to check laziness (dirty checking algorithm handles that)
updateStrings = new String[span];
for ( int j = 0; j < span; j++ ) {
updateStrings[j] = tableUpdateNeeded[j] ?
generateUpdateString( propsToUpdate, j, oldFields, j == 0 && rowId != null ) :
null;
}
}
else {
// For the case of dynamic-update="false", or no snapshot, we use the static SQL
updateStrings = getUpdateStrings(
rowId != null,
hasUninitializedLazyProperties( object, session.getEntityMode() )
);
propsToUpdate = getPropertyUpdateability( object, session.getEntityMode() );
}
for ( int j = 0; j < span; j++ ) {
// Now update only the tables with dirty properties (and the table with the version number)
if ( tableUpdateNeeded[j] ) {
updateOrInsert(
id,
fields,
oldFields,
j == 0 ? rowId : null,
propsToUpdate,
j,
oldVersion,
object,
updateStrings[j],
session
);
}
}
}
Here the decision is taken about which fields (of which tables) should be included into updating SQL query string.
In case of
dynamic-update="true" if the obejct's field is NOT marked as dirty (
"mark it for updating" - citation of initial question) the database table(s) field(s) will NOT be included into updating SQL.