Cambridge Technology PartnersCommunity Documentation

Chapter 6. Auditing

6.1. Activating Auditing
6.2. Using Auditing Annotations
6.2.1. Updating Timestamps
6.2.2. Who's Changing My Entities?

A common requirement for entities is tracking what is being done with them. CDI Query provides a convenient way to support this requirement.

Tip

CDI Query does not support creating revisions of entities. If this is a requirement for your audits, have a look at Hibernate Envers.

CDI Query uses an entity listener to update auditing data before entities get created or update. The entity listener must be activated before it can be used. This can either be done globally for all entities of a persistent unit or per entity.

Activation per persistence unit:


<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" version="2.0">
    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <entity-listeners>
                <entity-listener class="com.ctp.cdi.query.audit.AuditEntityListener" />
            </entity-listeners>
        </persistence-unit-defaults>
    </persistence-unit-metadata>
</entity-mappings>
        

Activation per entity:

@Entity

@EntityListeners(AuditEntityListener.class)
public class AuditedEntity implements Serializable {
    ...
}    

All that has to be done now is annotating the entity properties which are used to audit the entity.