Hullo, I've implemented an Interceptor to apply audit values (e.g. createdBy, createdDate). When a joined subclass entity is persisted the parent table record gets the audit values applied by the Interceptor but the child table record does not and in my case a not-null integrity constraint violation occurs. If I set the audit fields on the entity object before persisting, effectively bypassing the Interceptor, both parent and child records get the audit fields correctly.
Looking at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(...) there is the following comment and code before the Interceptor is called:
Code:
// Put a placeholder in entries, so we don't recurse back and try to save() the
// same object again. QUESTION: should this be done before onSave() is called?
// likewise, should it be done before onUpdate()?
source.getPersistenceContext().addEntry(
entity,
Status.SAVING,
null,
null,
id,
null,
LockMode.WRITE,
useIdentityColumn,
persister,
false,
false
);
This looks related to my problem but I don't understand enough of the internal logic to be sure. It does suggest that the logic could be reordered which could mean the audit values applied by the Interceptor are done before the parent and child table record values are assigned.
Here are my hibernate definitions:
Code:
<class name="EntityA" table="EntityA">
<id name="id" column="ID">
<generator class="native"/>
</id>
<!-- Properties -->
<!-- VxsAuditable attributes -->
<property name="createdBy" column="CREATED_BY_USER" update="false"/>
<property name="createdTimestamp" column="CREATED_TIMESTAMP" update="false"/>
<property name="updatedBy" column="UPDATED_BY_USER" insert="false"/>
<property name="updatedTimestamp" column="UPDATED_TIMESTAMP" insert="false" update="false"/>
</class>
<joined-subclass name="EntityB" extends="EntityA" table="EntityB">
<key column="ID"/>
<!-- Properties -->
<!-- VxsAuditable attributes -->
<property name="createdBy" column="CREATED_BY_USER" update="false"/>
<property name="createdTimestamp" column="CREATED_TIMESTAMP" update="false"/>
<property name="updatedBy" column="UPDATED_BY_USER" insert="false"/>
<property name="updatedTimestamp" column="UPDATED_TIMESTAMP" insert="false" update="false"/>
</joined-subclass>
Does anyone have any idea whether I'm doing something wrong or possible there could be a bug? Cheers