InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Versioning JPA Entities

Posted by Craig Wickesser on Jul 31, 2008

Sections
Architecture & Design,
Development,
Operations & Infrastructure
Topics
Versioning ,
Java ,
Data Access
Tags
Envers
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:
An old idea by Billy Newport Posted
Re: An old idea by Adam Warski Posted
EclipseLink's Historical Sessions by Ralph Poellath Posted
Re: EclipseLink's Historical Sessions by Adam Warski Posted
  1. Back to top

    An old idea

    by Billy Newport

    I've seen this pattern many times over the years but what tends to cause issues with this system is every update/remove becomes an insert and high speed inserting can be a challenge for most databases on the market. Databases are better at updating usually and performance can become an issue as a result. It's a cool idea though.

  2. Back to top

    EclipseLink's Historical Sessions

    by Ralph Poellath

    How does this compare to EclipseLink's Historical Sessions?

  3. Back to top

    Re: EclipseLink's Historical Sessions

    by Adam Warski

    Hello,

    from what I've read, EclipseLink first of all uses another JPA provider :) (Envers uses Hibernate)

    Then, it offers you two options:
    - use Oracle 9i which has versioning capabilities built-in
    - configure HistoryPolicy to read data from a schema that _you_ design

    With Envers, you don't need to be concerned with designing the schema. All necessary tables are generated automatically, you just specify that you want your entities versioned. This has its bad sides: less flexibility, as the way historical data is stored is fixed, and good sides: really easy to do if you want to version many entities, with almost no additional work. You can of course use all databases supported by Hibernate (almost all: as long as they support subqueries).

  4. Back to top

    Re: An old idea

    by Adam Warski

    Hello,


    that's true, when there really are lots and lots of updates, the inserts may be a bottleneck. But I designed Envers mainly with non-very-fast changing data in mind.


    Adam

Educational Content

Jesper Boeg on Priming Kanban

In this interview, Jesper Boeg, author of the new InfoQ book – Priming Kanban, discusses the keys to using Kanban effectively, and how to get started if you are currently using other approaches.

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.