Hibernate version: 3.1
Friends,
I am trying to create an Audit interceptor to track all entity changes in the application. This also includes tracking all new associations.
I am having problems with intercepting unidirectional one-to-many association. I tried this with some simple entities to work on the problem. Here are the mapping files :
Mapping documents:
Class : Client
Code:
<class name="com.test.Client" table="CLIENT" lazy="false">
<id name="clientNumber" type="integer" unsaved-value="null" >
<column name="clientNumber" not-null="true"/>
<generator class="increment"/>
</id>
<property column="title" name="title" type="string" />
<property column="lastName" name="lastName" type="string" not-null="true"/>
<property column="firstName" name="firstName" type="string" not-null="true"/>
<set name="references" inverse="false" cascade="delete">
<key column="client_id" />
<one-to-many class="Reference"/>
</set>
</class>
Class : ReferenceCode:
<class name="com.test.Reference" table="Reference" >
<id name="id" type="integer" unsaved-value="null" >
<column name="id" not-null="true"/>
<generator class="increment"/>
</id>
<property column="name" name="name" type="string" not-null="true"/>
</class>
Here is my test case :
Code:
public void testUnidirectionalAssociation() {
Client client = new Client();
client.setClientNumber(null);
client.setFirstName("Steve");
client.setLastName("Jobs");
client.setTitle("Mr");
dao.persist(client); // Client object is saved and flushed
Reference r = new Reference();
r.setName("Client type");
dao.persist(r); // Reference object is saved and flushed
client.addReference(r);
dao.persist(client);
}
Now, my AuditInterceptor's onSave() method gets called when the Client and Reference objects are created. But when there is just an association change, the onFlushDirty() methods do not get called.
We do have an onCollectionUpdate() callback which however sends across the entire collection. How do we find the newly added (or the "dirty") entities ?
Any help on how to track such association changes please ?
Thanks so much.[/code]