javahibernatehibernate-enversauditingspring-data-envers

Hibernate envers: how to get the current audited table name?


With Hibernate Envers, is it possible to get the current audited table? I took the example from the doc but I add an extra column tableName:

@Entity
@RevisionEntity(ExampleListener.class)
public class ExampleRevEntity extends DefaultRevisionEntity {
    private String username;
    private tableName;
    ...
}

And the listener:

public class ExampleListener implements RevisionListener {

    public void newRevision(Object revisionEntity) {
        ExampleRevEntity exampleRevEntity = (ExampleRevEntity) revisionEntity;
        ...
        exampleRevEntity.setUsername(...);

        exampleRevEntity.setTableName(...); // How to get the current table audited table?

    }
}

I found this old post Get audit table name from hibernate envers? but without a relevant answer. Thanks a lot


Solution

  • You could use the EntityTrackingRevisionListener which is the extension of the RevisionListener.

        public class ExampleRevEntityListener
                     implements EntityTrackingRevisionListener {
            @Override
            public void entityChanged(java.lang.Class entityClass, java.lang.String entityName, 
               java.io.Serializable entityId, RevisionType revisionType, java.lang.Object revisionEntity) {
    
                Table tableAnnotation = entityClass..getAnnotation(javax.persistence.Table.class).name();
            }