简体   繁体   中英

Hibernate Envers How to record additional audit data such as table name being audited

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?

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);
        }
    }

我不知道Envers是否可以开箱即用地跟踪要审核的记录的表名,但是我知道它可以跟踪实体名,而可以通过三种不同的方式启用

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM