Initial valid-time support in Envers

I just commited initial support for valid-time auditing in Envers, a feature that a lot of users has been (directly or indirectly) asking for. It’s joint work, as Stephanie Pau contributed a patch with a large portion of those changes – thanks!

You can try it out by checking out Hibernate trunk source code.

What is valid-time about? So far Envers only stored the revision at which a change was made. This information is enough to retrieve historical data, however the queries are quite complicated and in advanced use cases can be time-consuming. This can be improved when we store both the start and end revisions, that is information on when data was “valid”. For historic entities, both column are filled, and for “current” data, the end revision column is null.

Using the valid-time audit strategy, it will be possible to:

  • speed up and simplify the queries to retrieve historical data
  • implement support for queries, which traverse relations
  • implement other types of queries, like latest changes

To configure Envers to store the end-revision number, you have to specify a property in your configuration file:

<property name="org.hibernate.envers.audit_strategy">

Envers will then generate and additional REVEND column (next to the REV column) in every audit (_AUD) entity/table; however this column won’t be part of the primary key. You can change the name of the end-revision column by setting the org.hibernate.envers.audit_strategy_valid_time_end_name property value.

The value of the end-revision column can be calculated basing only on the original revision-changed columns, so using a couple of queries it will be possible to easily migrate existing data to the new audit strategy.

Please note that this feature is experimental, and can be changed in the future. The associated JIRA issue is HHH-3763.