How To do Auditing in a Spring Boot Application

Posted By : Gursahib Singh | 31-Oct-2018

The auditing in the spring boot can be achieved by using hibernate envers.
Steps to implement auditing in spring-boot using hibernate

envers:
1. The first step is to include the following dependency in the pom.xml file of the spring-boot application.

   <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-envers</artifactId>
      <version>${hibernate.version}</version>
   </dependency>

2. The next step is to audit the domain.This can be done by using @Audited annotation.

   @Entity
   @Audited
   public class Ticket {
	    @Id
	    @GeneratedValue(strategy = GenerationType.AUTO)
	    private Long id;
	    private String subject;
	    private String description;
	    @Enumerated(EnumType.STRING)
	    private Priority priority;
	    @Enumerated(EnumType.STRING)
	    private TicketStatus status;
	   // constructor
	   // getters and setters
   }

The fields of the domain which are required not to be audited can be annotated with @NotAudited annotation. This annotation will ensure that on any update and delete of this field, no record will be recorded in the audit table.

3. Run the application.

In the runtime, an entity_aud and ref info table will be created in the database. The entity_aud table contains all the columns of the entity table including an additional column named "revId" which is mapped with the column "id" of the rev-info table. The rev-info table is common for all entity-aud tables. Any change(create, update or delete) in the entity table will lead to the entry in the entity-aud table and the rev-info table. The rev-info table by default also contains the created date column to contain the date-time on which the entry is created.


It is sometimes required to store the auditor name and other information, for that the revInfo table is needed to be customized. To do so we need to create a domain class annotated with @RevisionEntity and define all the fields which we require in it as follows.

   @Entity
   @RevisionEntity(ConferenceRevisionListener.class)
   public class RevInfo {
	  @Id
	  @GeneratedValue
	  @RevisionNumber
	  private int id;

	  @RevisionTimestamp
	  private long timestamp;

	  private Long auditorId;
	  private String auditorName;
	// constructor
	// getters and setters
  }


By default, the hibernate will make an entry of id field and timestamp field in the revInfo table. For the entry of the customized fields defined in the revInfo table, we need to define a method in the customized revisionListener class which implements RevisionListener interface.
In this method we can set the values of the customized fields defined in the revInfo domain.
   import org.hibernate.envers.RevisionListener;
   import com.oodles.domain.RevInfo;
   public class ConferenceRevisionListener implements RevisionListener {

	   @Override
	   public void newRevision(Object revisionEntity) {
		   RevInfo entity=(RevInfo) revisionEntity;
		   entity.setAuditorId(AuditorDetails.auditorId);
		   entity.setAuditorName(AuditorDetails.auditorName);
	   }
   }

This is how auditing is done in a spring-boot application

 

 

 

About Author

Author Image
Gursahib Singh

Gursahib is a software developer having key skills in J2SE and J2EE. His hobbies are playing chess, reading and learning new softwares.

Request for Proposal

Name is required

Comment is required

Sending message..