Friday, June 19, 2015

Hibernate Envars, ValidityAuditStrategy Fails with Cannot update previous revision for entity

Symptom:

You have configured for the org.hibernate.envers.strategy.ValidityAuditStrategy with your Hibernate Envers to audit trail SQL table records with the following exception;

java.lang.RuntimeException: Cannot update previous revision for entity

Root Cause:

This happens when the table that already has records and you add the Envers auditing with ValidateAuditStrategy "after the fact". In other words, there are legacy records which were not originally audited.

Basically, the ValidityAuditStrategy was designed only with the assumption that you started using it from completely no data in the table and its related tables. It requires the initial INSERT record of the very first record of all rows with a unique record ID.

This issue has been logged in the Hibernate JIRA HHH-8456

Workarounds:

If you can afford to disable the ValidyAuditStrategy, that will log the entries.

The initial revision entry must be inserted into the newly created _AUD table for every existing legacy record.

This problem may be worked around by using the following tool that's available from the GitHub.

https://github.com/hydra1983/hibernate-envers-tools