We have a scenario where we need to perform several updates, but not for all columns.
Code:
Session session = ...
session.beginTransaction();
for () {
object.setPrimaryKey(key);
object.setProperty(value);
object.setAnotherProperty(value);
session.update(object);
if ( i % 3000 == 0 ) {
session.flush();
session.clear();
}
}
.. commit ...
Regardless of how many properties are present, hibernate generates an UPDATE sql for all columns which will fail for a column with a NON-NULL constraint, for which we aren't setting anything. Even if there is no constraint, we do not wish to wipe out the existing value. To work around, we first load the object and update only the selected value.
I have noticed that the batch is fast if we can avoid the load.
Is there any better way to achieve this ? We can try out the conventional ways and cache the objects, but does hibernate offer anything we might have missed here.