Sunday, February 21, 2010

Auditing with hibernate envers

I used the weekend to play around with hibernate envers. Envers is a framework that enables you to audit your database changes with hibernate. It was really easy to setup, just add the jar and set some configuration parameters. You have to add the envers listeners to your EntityManagerFactory and from then on every entity annotated with @Audited stores all changes in a seperate audit table. Envers enables you to look at each revision of your entity and track back your complete history. Since Envers stores eachs revision completely, I wrote some code to make a diff on my entities. That was quite tricky, because of my entities equals and hashcode methods compare id and version. Since different revisions have different versions I could not rely on that to work. But with some "frickeling" I made it work in a day. I now can show the change history of my Objects, you can even see the user that made the changes (a little spring aspect sets a threadlocal on the envers audit listener).

Envers will be part of hibernate 3.5, and as I have seen so many tricky auditing solutions before, I guess it could become a standard for hibernate auditing issues.


Anonymous said...

I didn't tried using it, but there's also a new project that could be used for what you need:
Hibernate History

Majrooh said...


Im trying to use envers as well on my project. I am using classical hibernate meaning im using hbm.xml files for my pojos to be mapped to tables. I added the @audited on my pojo and the event listeners in my hibernate.cfg.xml. (My session factory is created within spring using annotatonsessionfactoprybean in sprin-config.xml)

Envers creates the _aud table for my table but does not add anything upon update or insert.

Did you come across this issue by any chance.

my jboss is 5.1 hibernate 3.5.3 and using it with spring.

Will appreciate a reply.

Thomas Kratz said...

Hi Majrooh, I sadly can't tell. I'm using JPA Api and it works perfect. Maybe you should ask on the envers forum ?

Using Mapstruct with Protobuf3