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:

1
2
3
<property name="org.hibernate.envers.audit_strategy">
   org.hibernate.envers.strategy.ValidTimeAuditStrategy
</property>

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.

Adam

  • Pingback: Blog of Adam Warski()

  • Pingback: Whiteship's Epril » Blog Archive » [하이버네이트] Hibernate Core 3.6.0.Final 나왔구나()

  • Stefan

    Hi Adam,

    i have an application which make use of this auditStrategy. while the revend column is created and populated properly, im not sure on how to query with it, since the auditreader not ships with methods to query it(if im not wrong). so my question is, is this strategy implicitly used by envers when i e.g. query for entities at a given revision? if not, how can i explicitly enable it in the auditReader?

    TIA
    Stefan

  • It is implicitly used by the AuditReader. So you don’t have to do anything special.

    Adam