Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Versioning JPA Entities

Versioning JPA Entities

This item in japanese

Envers is an open-source project from JBoss which recently released version 1.0.0 GA. The primary goal of the project, as stated on the project website is to,
...enable easy versioning of persistent JPA classes. All that you have to do is annotate your persistent class or some of its properties, that you want to version, with @Versioned. For each versioned entity, a table will be created, which will hold the history of changes made to the entity. You can then retrieve and query historical data without much effort.
Envers works wherever Hibernate can run as Envers works with Hibernate and Hibernate Entity Manager.  For example Envers can run in a standalone environment, inside an application server, with JBoss Seam or Spring. The current release of Envers offers a number of features:
  • versioning of basic properties (strings, integers, longs...)
  • versioning of embedded components, which are composed of basic properties
  • versioning of classes with simple, composite and embedded ids
  • versioning of one-to-one uni- (only on the owning side) and bi-directional relations
  • versioning of one-to-many uni- (only on the owning side) and bi-directional relations
  • support for secondary tables
  • logging data for each revision using a "revision entity"
  • querying historical data
Specifically the 1.0.0 GA release offers revisioning when a collection changes, which wasn't available in the preview and beta versions. For example,
Imagine you have a Person and Address entities in a bidirectional many-to-one relation (each person has exactly one address and many people can live at the same address). Now you change an address of a person - hence, the content of the collection of persons for the new and old addresses change. In the preview and beta versions of Envers, no revision will be generated for the Addresses (only for Person), as data in the database tables is not modified (but data in the java beans is modified). Now, however, a revision will be created for all three entities.
If for some reason that functionality is not desired it can be disabled by making a small configuration change,
<property value="false" name="org.jboss.envers.revisionOnCollectionChange"/>

Future versions of Envers will include the ability, version arbitrary relationships, store only diffs between revisions (helping to save space), and more. Envers is a fairly new project and hasn't gained a lot of traction yet but it is certainly worth keeping an eye on. For additional information try the following resources:

Rate this Article