In order to get the dirty properties to the onFlashDirty, perform the next steps:
1) Extend the DefaultFlushEntityEventListener as follows:
Code:
public class FlushEntityEventListenerDelegator<T> extends DefaultFlushEntityEventListener {
@Override
protected boolean handleInterception(FlushEntityEvent event) {
SessionImplementor session = event.getSession();
EntityEntry entry = event.getEntityEntry();
EntityPersister persister = entry.getPersister();
Object entity = event.getEntity();
// give the Interceptor a chance to modify property values
final Object[] values = event.getPropertyValues();
final boolean intercepted = invokeInterceptor(session, event);
// now we might need to recalculate the dirtyProperties array
if (intercepted && event.isDirtyCheckPossible() && !event.isDirtyCheckHandledByInterceptor()) {
int[] dirtyProperties;
if (event.hasDatabaseSnapshot()) {
dirtyProperties = persister
.findModified(event.getDatabaseSnapshot(), values, entity, session);
} else {
dirtyProperties = persister.findDirty(values, entry.getLoadedState(), entity, session);
}
event.setDirtyProperties(dirtyProperties);
}
return intercepted;
}
/**
* This method will invoke the onFlushDirty with parameters
* containing the dirty data.
*
* @param session
* @param event
* @return
*/
protected boolean invokeInterceptor(SessionImplementor session, FlushEntityEvent event) {
if (event.getDirtyProperties() == null) {
return false;
}
EntityEntry entry = event.getEntityEntry();
EntityPersister persister = entry.getPersister();
Object entity = event.getEntity();
final Object[] values = event.getPropertyValues();
// Convert the values, properties names (old and new values)
// and types to arrays that contains the dirty ones.
int[] dirtyPropertiesIndexes = event.getDirtyProperties();
int dirtyPropertiesLength = dirtyPropertiesIndexes.length;
String[] dirtyPropertiesNames = new String[dirtyPropertiesLength];
Object[] dirtyPropertiesValues = new Object[dirtyPropertiesLength];
Object[] loadedPropertiesValues = new Object[dirtyPropertiesLength];
Type[] dirtyPropertiesTypes = new Type[dirtyPropertiesLength];
for (int i = 0; i < dirtyPropertiesLength; i++) {
int dirtyPropertyIndex = dirtyPropertiesIndexes[i];
dirtyPropertiesNames[i] = persister.getPropertyNames()[dirtyPropertyIndex];
dirtyPropertiesValues[i] = values[dirtyPropertyIndex];
dirtyPropertiesTypes[i] = persister.getPropertyTypes()[dirtyPropertyIndex];
loadedPropertiesValues[i] = entry.getLoadedState()[dirtyPropertyIndex];
}
// Invoke the intercepter.onFlushDirty method.
return session
.getInterceptor()
.onFlushDirty(entity, entry.getId(), dirtyPropertiesValues, loadedPropertiesValues, dirtyPropertiesNames, dirtyPropertiesTypes);
}
/**
* This method was replaced with the invokeInterceptor(SessionImplementor session, FlushEntityEvent event)
* method.
*
* @deprecated
*/
@Deprecated
@Override
protected boolean invokeInterceptor(SessionImplementor session, Object entity, EntityEntry entry,
final Object[] values, EntityPersister persister) {
throw new UnsupportedOperationException(
"Since this method was replaced with another one it was not suppose to be invoked.");
}
}
2) Register the above listener to the hibernate session by performing the next steps:
1) org.hibernate.cfg.Configuration configuration
.getEventListeners()
.setFlushEntityEventListeners(new FlushEntityEventListenerDelegator[] { new FlushEntityEventListenerDelegator<T>() });
2) org.hibernate.SessionFactory hibernateSessionFactory = configuration.buildSessionFactory();