Storing/Importing historical data with Hibernate Envers


#1

Hi there!

I am evaluating Hibernate Envers atm for use in a new product. For daily business tasks, envers would be a really smart solution, as it would audit any change made.

But what about the import from an old product with historical data? In my understanding, Envers is made for auditing, more or less like e.g. git. You have a change (e.g. customer moved), which results in a new revision with the current timestamp. But what about historical information during an import, where I get information like:

  • from 2006 - 2010: Customer lived in NYC
  • from 2010 - 2014: Customer lived in LA
  • from 2014 - now: Customer lives in Miami

Is it possible with Hibernate Envers to store revisions for the past? Or should a newer revision always have a newer timestamp than the old one?

Thank you for any help!


#2

Yes, but this is only possible by you writing a data migration step that effectively imports and seeds the Envers schema with the historical data from the legacy source before using Envers for day-to-day audit operations.

Depending on the schema, this can quickly go from a trivial task to quite complex.

A trivial migration script might look something like this:

INSERT INTO REVINFO (REV,TIMESTAMP) values ( NextRevisionId, 2006-ts-value )
INSERT INTO CUSTOMER_AUD (...) VALUES (...)

INSERT INTO REVINFO (REV,TIMESTAMP) values ( NextRevisionId, 2010-ts-value )
INSERT INTO CUSTOMER_AUD (...) VALUES (...)

INSERT INTO REVINFO (REV,TIMESTAMP) values ( NextRevisionId, 2014-ts-value )
INSERT INTO CUSTOMER_AUD (...) VALUES (...)

Hope that helps.
Chris