Hi,
I'm using an interceptor to update some auditing properties of the entities when the entity is saved/updated. I expected hibernate to execute only one SQL statement (INSERT), however it executes two (one INSERT and one UPDATE). This behavior occurs in both onSave() and onFlushDirty() methods.
Is this the correct behavior? I know that entity updates after a call to Session.save() generate UPDATE statements, but is the entity state already "saved" when onSave() is called?
The interceptor code:
Code:
public boolean onFlushDirty(Object entity, Serializable id,
Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types)
throws CallbackException {
if (entity instanceof SimVersionedEntity) {
updateSimEntity(propertyNames, currentState);
return true;
} else
return false;
}
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types)
throws CallbackException {
if (entity instanceof SimVersionedEntity) {
updateSimEntity(propertyNames, state);
return true;
} else
return false;
}
private void updateSimEntity(String[] propertyNames,
Object[] currentState) {
final Date now = new Date();
for (int i = 0; i < propertyNames.length; i++) {
if ("updtDate".equals(propertyNames[i])
|| "updtTime".equals(propertyNames[i])) {
currentState[i] = now;
} else if ("userCode".equals(propertyNames[i])) {
currentState[i] = new Long(1);
}
}
}