1

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

akuma8
  • 4,160
  • 5
  • 46
  • 82

1 Answers1

0

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();
        }
Rohit
  • 2,132
  • 1
  • 15
  • 24
  • Thanks I'll try that. – akuma8 Jun 15 '20 at 17:34
  • I gave a try but this is not enough because many entities could be involved in the same transaction. With this solution only the last table is taken. I have to look for another solution. – akuma8 Jun 16 '20 at 17:32
  • It should be called for all the audited entities. how many entities are persisted in the transaction? – Rohit Jun 18 '20 at 16:47
  • Sure it is called for all audited entities but only the last one is captured. It's a normal behaviour I think. In my case, 3 entities are involved and the last one wins. – akuma8 Jun 18 '20 at 21:21