Envers get previous revision with Audit API


I am using hibernate-envers 5.5.0.Final with:

  • audit_strategy: org.hibernate.envers.strategy.ValidityAuditStrategy
  • @Audited(withModifiedFlag=true)

The audit tables are well created and fed. I can see the field revend which references the revinfo table and marks the last revision for which this entity snapshot was still valid.

I am using the following code to retrieve details about the revisions for a given class.

var query = this.getAuditReader().createQuery().forRevisionsOfEntityWithChanges(classInstance, true);
var results = query.getResultList();

 for (var res : results) {
                var result = (Object[]) res;
                Object instance = result[0];
                RevisionEntityImpl revEntity = (RevisionEntityImpl) result[1];
                RevisionType revType = (RevisionType) result[2];
                Set<String> changedProperties = (Set<String>) result[3];

The select query generated by hibernate mentions the fields “revend” but
I am unable to see it in the objects returned by the API.

Where to find it?

Thanks for your help.

You have to enable the org.hibernate.envers.audit_strategy_validity_store_revend_timestamp configuration. Also see the documentation about this: Hibernate ORM 5.5.4.Final User Guide

Thanks @beikov , I have enabled it and have a timestamp column in each audit table.
It is also visible in the query generated by Hibernate when I call the API method forRevisionsOfEntityWithChanges().
However, I am unable to see those two fields in the object response provided query.getResultList().

I think you might have to provide a custom RevisionEntity then.

Hi @beikov, It is possible to give more input, please?
The information is queried by hibernate, how to map it back to the object response of the api ?

If you map a custom RevisionEntity, you can map the column to an attribute. Since it is configurable, I don’t think that there is an out of the box way to refer/access the attribute/column.