AdBlock Detected

It looks like you're using an ad-blocker!

Our team work realy hard to produce quality content on this website and we noticed you have ad-blocking enabled. Advertisements and advertising enable us to continue working and provide high-quality content.

Auditing on Spring Data

In this new post on Auditing with Annotations in Spring Data, we will see how we can audit certain fields in our table using annotations alone. In a previous post, we also saw how to create a project with Spring Data.

These annotations are inspired by SQL triggers that trigger an event to perform the required insertion in the database.

Auditing with Spring Data JPA

Adding Spring Data Dependency in Maven

Spring Data is a module of Spring that extends JPA, adding JPA functionalities and abstracting them for us. To add Spring Data to our application, we add the following Maven dependency:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
</dependency>

Enabling JPA Auditing

The first step to enable auditing in our Spring Data application is to activate it. To activate auditing, we add @EnableJpaAuditing to our @Configuration class.

@Configuration
@EnableJpaRepositories
@EnableJpaAuditing
public class SpringDataConfig { ... }

Adding the @EnableJpaAuditing annotation is essential for Spring Boot to recognize the auditing annotations for our database.

Adding Entity Listener

Next, we’ll add an @EntityListeners to capture and audit information during the persistence and updating of our entities. In this process, we’ll use the pre-existing Spring AuditingEntityListener class. The configuration looks like this:

@Entity
@EntityListeners(AuditingEntityListener.class)
public class Car { ... }

Automatically Create and Update Dates with Spring Data

Two of the annotations most commonly used with Spring Data are undoubtedly @CreatedDate and @LastModifiedDate. These annotations automatically add the creation date and modification date to a record.

@Entity
@EntityListeners(AuditingEntityListener.class)
public class Car {
    
    @Id
    private Long id;
    
    @Column(name = "created_date")
    @CreatedDate
    private long createdDate;

    @Column(name = "modified_date")
    @LastModifiedDate
    private long modifiedDate;
    
    //...
    
}

Adding Creation and Modification Dates to All Entities

In general, we would want creation and modification dates to be handled in all our entities. For such cases, we can achieve this through inheritance in a single class using the @MappedSuperclass annotation.

@MappedSuperclass
@Getter
@Setter
public class Audit {
 
    @Column(name = "created_date")
    @CreatedDate
    private long createdDate;

    @Column(name = "modified_date")
    @LastModifiedDate
    private long modifiedDate;
 
}

@Getter
@Setter
@Entity
public class Car extends Audit {
 
    private String model;
 
....
}

Auditing the Author when Spring Security is Enabled

When Spring Security is enabled in our application, we can not only authorize and authenticate a user but also audit who is making modifications or creating a record in our database.

To audit the user, we use two different annotations: @CreatedBy and @LastModifiedBy. These two fields are populated with the Spring Security Principal.

@Entity
@EntityListeners(AuditingEntityListener.class)
public class Car {
       
    @Column(name = "created_by")
    @CreatedBy
    private String createdBy;

    @Column(name = "modified_by")
    @LastModifiedBy
    private String modifiedBy;
    
    
}

In many cases, we might want to add additional logic when saving user information. In those cases, we can implement AuditorAware, which allows us to override some of its methods and add the necessary information.

public class AuditorAwareImpl implements AuditorAware<String> {
 
    @Override
    public String getCurrentAuditor() {
        // here more code
    }

}

To use the AuditorAware functionality, we need to activate JPA auditing with the auditorProvider. Use @EnableJpaAuditing(auditorAwareRef="auditorProvider") and create a bean of this type to initialize the class.

@EnableJpaAuditing(auditorAwareRef="auditorProvider")
public class SpringDataConfig {
    
    @Bean
    AuditorAware<String> auditorProvider() {
        return new AuditorAwareImpl();
    }     
    
}

Conclusion

In this post on Auditing with Annotations in Spring Data, we have seen how to use various annotations with Spring Boot that will help and simplify our data auditing work. Undoubtedly, you will frequently use this feature of Spring Data in your projects.

You can find an example in this GitHub link.

Leave a Reply

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