Hibernate Envers Tutorial
Introduction to Hibernate Envers
Hibernate Envers is a powerful auditing and versioning framework for Hibernate. It allows developers to track changes made to entity data over time, enabling them to retrieve historical data and understand how the entity state has evolved. This feature is particularly useful for applications that require data integrity, compliance, or historical data analysis.
Getting Started with Hibernate Envers
To use Hibernate Envers, you need to include the Envers library in your project. If you are using Maven, add the following dependency to your pom.xml
file:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-envers</artifactId> <version>5.4.30.Final</version> </dependency>
After adding the dependency, ensure that you have configured Hibernate properly in your application.
Annotating Entities for Auditing
To enable auditing on an entity, you need to annotate your entity class with @Audited
. Here is an example of an entity class:
import javax.persistence.*; import org.hibernate.envers.Audited; @Entity @Audited public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Double price; // Getters and Setters }
In this example, changes to the Product
entity will be tracked by Envers.
Configuring Envers
You can configure Envers settings in your persistence.xml
or Hibernate configuration file. Here is an example configuration:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1"> <persistence-unit name="myUnit"> <properties> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.envers.store_data_at_delete" value="true"/> </properties> </persistence-unit> </persistence>
Retrieving Audit Information
To retrieve audit information, you can use the AuditReader
class provided by Envers. Here’s how to do it:
import org.hibernate.envers.AuditReader; import org.hibernate.envers.AuditReaderFactory; // Assume we have a session object AuditReader auditReader = AuditReaderFactory.get(session); Product product = auditReader.find(Product.class, productId, revision);
This code retrieves the Product
entity based on its ID and a specific revision.
Viewing Audit History
To view the audit history of a given entity, you can use the getRevisions
method:
List<Number> revisions = auditReader.getRevisions(Product.class, productId); for (Number revision : revisions) { System.out.println("Revision: " + revision); }
Conclusion
Hibernate Envers is a robust solution for auditing and versioning in Java applications. By following the steps outlined in this tutorial, you can easily implement auditing features in your application and maintain a history of changes made to your entities.