Skip to content

mtumilowicz/envers-revision-entity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

envers-revision-entity

Reference: Revision log jboss doc
Reference: RevisionEntity tutorial
Reference: Advanced querying envers
Reference: AuditQueryCreator doc
Reference: AuditQuery doc

preface

Take a look at pt.1

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.

details

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)
    ... ... ...

manual

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()
    

project details

  • RevisionEntity: com.example.envers.audited.domain.customer.model.CustomRevisionEntity
  • RevisionListener: com.example.envers.audited.infrastructure.revision.listener.CustomRevisionEntityListener

About

Exploring basic features of @RevisionEntity from Hibernate Envers.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published