Hi,
I'm using a session interceptor to detect inserts and updates.
When one occurs I do set the dateCreated and dateModified of the entity if the entity is auditable.
Here's the onSave method
Code:
public boolean onSave( Object entity, Serializable id, Object[] currentState, String[] propertyNames, Type[] types )
throws CallbackException
{
boolean returnValue = false;
if ( entity instanceof HibernateAuditableEntity )
{
HibernateAuditableEntity hae = (HibernateAuditableEntity)entity;
hae.setDateCreated( new Timestamp( System.currentTimeMillis() ) );
//returnValue = true;
}
return returnValue;
}
similarly for onFlushDirty()
This approach works great for entities that have no collection ( one to many relationship) .
I stepped into Hibernate code and it seems that for entities with collections there is a forced substitution
Here the code from AbstractSaveEventListener.java
Code:
protected Serializable performSaveOrReplicate(
Object entity,
EntityKey key,
EntityPersister persister,
boolean useIdentityColumn,
Object anything,
EventSource source)
throws HibernateException {
{
....
boolean substitute = substituteValuesIfNecessary(entity, id, values, persister, source);
if ( persister.hasCollections() ) {
substitute = substitute || visitCollectionsBeforeSave(id, values, types, source);
}
if (substitute) persister.setPropertyValues( entity, values, source.getEntityMode() );
My interceptor is called in the substituteValuesIfNecessary method() and I do return false in onSave. Since the persistor detects that there is a collection in the current entity it calles visitCollectionBeforeSave that returns true and forces an override of the just set dateCreated with a null.
The collection is defined as below in the entity mapping
Code:
<set name="roles" cascade="all" inverse="true" lazy="true" >
<key column="PARTICIPANT_ID" />
<one-to-many class="Role" />
</set>
Is this the expected behaviour and how can I bypass this annoyance ?
BTW this happens only when I do inserts with entities that have list(s).
Regards,
Q