3.4.8.2. Developing custom auditing
For real use case, an application will prefer more than printing the changed instances.
The application, in such case, needs to implement org.apache.openjpa.audit.Auditor interface.
This simple interface has the following method:
/**
* OpenJPA runtime will invoke this method with the given parameters
* within a transaction.
*
* @param broker the active persistence context.
* @param newObjects the set of auditable objects being created. Can be empty, but never null.
* @param updates the set of auditable objects being updated. Can be empty, but never null.
* @param deletes the set of auditable objects being deleted. Can be empty, but never null.
*/
public void audit(Broker broker, Collection<Audited> newObjects, Collection<Audited> updates,
Collection<Audited> deletes);
OpenJPA runtime will invoke this method before database commit.
Via this callback method, the application receives the auditable instances in three separate collections of org.apache.openjpa.audit.Auditable.
An Auditable instance provides the latest and original state of a persistent object.
The latest object is the same persistent instance to be committed.
The original instance is a transient instance holding the original state of the instance when it entered the managed context.
The active persistence context is also supplied in this callback method, so that an application may decide to persist the audit log in the same database.
It is important to note that the original object can not be persisted in the same transaction, because it has the same persistent identity of the latest object.
A single instance of implemented org.apache.openjpa.audit.Auditor interface is available for a persistence unit.
However, an application’s own implementation of this interface need not be thread-safe, because OpenJPA runtime guards against concurrent invocation of the callback method.
The org.apache.openjpa.audit.Auditor interface is configurable.
Hence any bean style getter and setter method on its implementation will be populated as usual for any other OpenJPA plugin.
In the following example,
<property name="openjpa.Auditor" value="com.acme.Auditor(param2=10,param2='hello')"/>
An instance of com.acme.Auditor will be instantiated and if it has been style getter and setter methods for param1 and param2, then the respective setters will be called with 10 and "hello" before the instance being used.