Hibernate Envers is a module that implements auditing and versioning of persistent entities. Auditing and versioning is a key component for building production-level Spring Boot Microservices. Hibernate Envers integrates seamlessly with Spring Boot to achieve the same.

In this post, we will integrate Hibernate Envers in our Spring Boot Starter application.

Database Audit is important for compliance purposes. Also, many times, production issues have to be analyzed. For such an analysis, audit data is extremely useful.

Hibernate Envers POM Dependency

First step to use Hibernate Envers with Spring Boot is to include the dependency in the POM file.

<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-envers</artifactId>
			<version>5.3.1.Final</version>
</dependency>

Enable Auditing for Hibernate Entity

The next step is to simply enable auditing for the required entities. This can be easily accomplished by using @Audited annotation.

@Entity
@Table(name = "vehicle")
@Audited
public class Vehicle {

    @Id
    private UUID id;

    private String vehicleIdentityNumber;

    private String make;

    private String model;

    private String status;

    public Vehicle() {
    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public String getVehicleIdentityNumber() {
        return vehicleIdentityNumber;
    }

    public void setVehicleIdentityNumber(String vehicleIdentityNumber) {
        this.vehicleIdentityNumber = vehicleIdentityNumber;
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Vehicle{" +
                "id=" + id +
                ", vehicleIdentityNumber='" + vehicleIdentityNumber + '\'' +
                ", make='" + make + '\'' +
                ", model='" + model + '\'' +
                ", status='" + status + '\'' +
                '}';
    }
}

Just by annotating the entity class, Envers Hibernate registers such classes for Auditing. In other words, it means that apart from the main tables, audit tables will also be created for such entities.

To create the actual tables, however, the most straightforward approach is to use hibernate settings. This is by using the hibernate.hbm2ddl.auto option and setting it to create.

This setting is done in application.properties file.

spring.datasource.url=jdbc:h2:mem:mytestdb
spring.datasource.driverClassName
=org.h2.Driver
spring.datasource.username
=sa
spring.datasource.password
=
spring.jpa.hibernate.ddl-auto=create

Note that Hibernate auto create option is not safe for production.

Hibernate Envers in action

We can see hibernate audit logging in action by starting up the Spring Boot application. Once the application starts up, we can visit h2-console

hibernate envers h2 console

So what’s going on here?

As you can see, we have the expected VEHICLE table. Apart from that, we also have a new table called VEHICLE_AUD. The audit tables contain all the fields from the original table.

However, two new fields are also added. First is the REVTYPE field. This contains “0” for adding, “1” for updating, “2” for deleting. The other field is the REV field.

There is another table that gets generated. It is called REVINFO. It contains two fields REV and REVTSTMP. REV field in the VEHICLE_AUD is basically a foreign key to REV field in REVINFO table. The REVINFO tables keeps track of every transaction.

Hibernate Envers in Action

To test Hibernate Envers and audit logging, we can trigger a few transactions using our Vehicle end-points.

When we create a new record using something like the POST operation followed by updating it it using PUT operation, we get two records in the VEHICLE_AUD table.

hibernate audit logging

As you can see the ID is c7a2ac1750fd4e12877efc76b3743646.

There are two records with this ID. One having REVTYPE of 0 (Create) while another having REVTYPE of 1 (Update).

hibernate revinfo

The REVINFO table keeps track of all the revisions. If you see closely, the REV field in VEHICLE_AUD table is foreign key for REV field in REVINFO table.

Conclusion

Hibernate Envers is a very robust solution for audit logging needs in a Spring application.

It also fits seamlessly with Spring Boot. This allows hibernate audit logging to be enabled for your Spring Boot Microservices. Setting it up is pretty easy just by using annotations and including the dependency in the POM file.

The code for this post is now available on Github.


Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *