0

I have implemented a solution of Hibernate Envers.

I am extending RevisionLister by creating my own class to store the system username:

    import org.hibernate.envers.RevisionListener;

    public class CustomRevisionListener implements RevisionListener {

        public void newRevision(Object revisionEntity) {
            CustomRevisionEntity revision = (CustomRevisionEntity) revisionEntity;
            revision.setUsername(System.getProperty("user.name")); // for testing

        }

    }

This does the job, but what I want to do, is to make a more comprehensive record, that would include the table name being audited.

Does anyone know how I could do this. I cannot find any documentation relating to recording the table name?

babb
  • 423
  • 2
  • 15
  • 38

3 Answers3

3

See example 15.2 in the envers doc how to get the modified entity class(es). Then slightly change the code to obtain the table name from the entity class (assumes you use JPA/Hibernate annotations on entity classes):

public class CustomEntityTrackingRevisionListener
             implements EntityTrackingRevisionListener {
    @Override
    public void entityChanged(Class entityClass, String entityName,
                              Serializable entityId, RevisionType revisionType,
                              Object revisionEntity) {
        // either javax.persistence.Table or org.hibernate.annotations.Table
        Table tableAnnotation = entityClass.getAnnotation(Table.class);
        if (tableAnnotation != null)
          String tableName = tableAnnotation.getName();
          ((CustomTrackingRevisionEntity)revisionEntity).addTable(tableName);
        }
    }
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
1

I don't know if Envers can track the table name of the records being audited out of the box , but I know it can track the entity name instead which can be enabled by three different ways

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
0

You can extend DefaultTrackingModifiedEntitiesRevisionEntity, or configure org.hibernate.envers.track_entities_changed_in_revision parameter to true.

See Envers Doc: Tracking entity names modified during revisions

M-Zaiady
  • 176
  • 2
  • 9