Reference: Revision log jboss doc
Reference: RevisionEntity tutorial
Reference: Advanced querying envers
Reference: AuditQueryCreator doc
Reference: AuditQuery doc
Envers provides an easy way to log additional data for each revision. You simply need to annotate one entity with
@RevisionEntity
, and a new instance of this entity will be persisted when a new revision is created
(that is, whenever an audited entity is modified).
As revisions are global, you can have at most one revisions entity.
Remark: The revision entity must be a mapped Hibernate entity.
Remark: In case there is no entity annotated with @RevisionEntity
,
a default table will be generated, with the name REVINFO
.
This revision entity must have at least two properties:
- an
integer
/long
-valued property, annotated with@RevisionNumber
. long
/j.u.Date
- valued property, annotated with@RevisionTimestamp
. Value of this property will be automatically set by Envers.
Remark: You can either add these properties to your revision entity, or extend
org.hibernate.envers.DefaultRevisionEntity
, which already has those two properties.
To fill the revision entity with additional data, you'll need to implement the
org.jboss.envers.RevisionListener
interface.
Its newRevision
method will be called when a new revision is created, before persisting the revision entity.
The implementation should be stateless and thread-safe. The listener then has to be attached to the revision
entity by specifying it as a parameter to the @RevisionEntity
annotation.
-
RevisionEntity (
REVINFO
)ID TIMESTAMP ADDITIONAL FIELDS (ex. LOGIN) ... ... ...
We give two examples of creating queries (com.example.envers.audited.domain.customer.history.repository.CustomerHistoryRepository
):
wasEntityDeletedBy
customerAuditReader.get() .createQuery() .forRevisionsOfEntity(Customer.class, true) // include delete .addProjection(AuditEntity.id()) // maps to ids .add(AuditEntity.id().eq(id)) // take revisions of entity with id = id .add(AuditEntity.revisionType().eq(RevisionType.DEL)) // only DEL .add(AuditEntity.revisionProperty("login").eq(login)) // only by login .getResultList()
allIdsOfCustomersCreatedBy
auditReader .createQuery() .forRevisionsOfEntity(Customer.class, true, false) // full entities, not include deleted .add(AuditEntity.revisionType().eq(RevisionType.ADD)) // only ADD .add(AuditEntity.revisionProperty("login").eq(login)) // only by login .getResultList()
RevisionEntity
:com.example.envers.audited.domain.customer.model.CustomRevisionEntity
RevisionListener
:com.example.envers.audited.infrastructure.revision.listener.CustomRevisionEntityListener