Hibernate.org Community Documentation

- Prev

## Chapter 5. Tutorial Using Envers

This tutorial is located within the download bundle under `envers` and illustrates

- configuring Envers

- using the Envers APIs to look back through history

## 5.1. `persistence.xml`

This file was discussed in the JPA tutorial, and is largely the same here. The major difference is the set of properties defining listeners. Essentially this enables Envers to recieve notfications from Hibernate processing of certain events such as an entity being saved or updated.

## 5.2. The annotated entity Java class

Again, the entity is largely the same as seen in the JPA tutorial. The major difference to notice is the addition of the `@org.hibernate.envers.Audited` annotation which tells Envers to automatically track changes to this entity.

## 5.3. Example code

Again, this tutorial makes use of the JPA APIs. However, the code also makes a change to one of the entites and then uses the Envers API to pull back the initial revision (version) as well as the updated revision.

Example 5.1. Using the `org.hibernate.envers.AuditReader`

    public void testBasicUsage() {

        ...

        AuditReader reader = AuditReaderFactory.get( entityManager );

        Event firstRevision = reader.find( Event.class, 2L, 1 );

        ...

        Event secondRevision = reader.find( Event.class, 2L, 2 );

        ...

    }

First an `org.hibernate.envers.AuditReader` is obtained from the `org.hibernate.envers.AuditReaderFactory` wrapping the `javax.persistence.EntityManager`.

Then the `find` method is used to retrieve specific revisions of the entity. The first call reads "find revision number 1 of Event with id 2". The second call reads "find revision number 2 of Event with id 2".

Copyright © 2004 Red Hat, Inc.

- PrevChapter 4. Tutorial Using the Java Persistence AP...
- Top of page
- Front page