tags:

views:

44

answers:

2

Hi,

For a Java Web Application (Struts / Spring / Hibernate), Hibernate Event Listeners are new feature for Audit logging.

When there is need to capture some extra information such as the User who updated an Entity, details of what entity attributes changed compared to preview entity attributes etc, are there best practice patterns that can be followed ?

Example : For the User related information, if every entity maintains change-log attributes (UpdatedBy, CreatedBy), then the entity itself can provide the info.

For comparison of what entity attributes changed compared to previous state of entity in DB, is there no way other than comparing all the entities attributes before inserting ? (This seems a redundant measure)

A: 

The Hibernate Reference documentation provides a AuditInterceptor as an example for the implementation of a Hibernate Interceptor.

You can extend the EmptyInterceptor and override the method boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) that has a reference to the current object and its id, the content of its fields in the current and the previous state and all fieldnames and types.

With a Spring powered application you can configure these interceptors in your Spring application context where you configure the Hibernate SessionFactory.

codescape
A: 

If you need it specifically for auditing, there is no need to reinvent the wheel - take a look at JBoss Envers

Bozho
Thanks. Will try this out !
Icarus