Spring Boot Data JPA — Part III (Auditing)

Mahesh Bonagiri
3 min readJul 23, 2021

Part I (Getting started)|Part II(Pagination & Sorting)|Part IV (Projections)

In Previous posts I explained how to get started, Pagination & Sorting with Spring boot Data JPA. In this post we will discuss how to use Auditing with Spring Data.

In the context of ORM, auditing means tracking and logging events related to persistent entities, or simply entity versioning. Spring Data provides sophisticated support to transparently keep track of who created or changed an entity and the point in time this happened.

Let’s start:

we will use Spring Initializer generate the base for our project, in my previous post I have explained how to get starting with Spring Boot + Eclipse.

Define a Simple Entity

Lets create an Entity called BookEntity as shown below.

Each of our Book instances has a unique identifier: id, its name, author and its price associated with it.

Repository Interface:

Let’s have a look at our BookRepoisitory, which extends the CrudRepository,

Enable JPA Audit:

To start, In order to enable auditing we need to just add @EnableJpaAuditing on our @Configuration class as shown below.

Spring’s Entity Callback Listener

@EntityListeners will be the one that is responsible to listen to any create or update activity, Spring Data provides its own JPA entity listener class AuditingEntityListener. Let’s update our BookEntity with the listener.

Auditing Created and Last Modified Dates

By annotating a column with @CreatedDate we will inform Spring that we need this column to have information on when the entity is created. While @LastModifiedDate column will be defaulted to @CreatedDate and will be updated to the current time when the entry is updated. Generally, we would move the properties to a base class (annotated by @MappedSuperClass) which would be extended by all audited entities. In our example, we add them directly to BookEntity for the sake of simplicity.

Update SpringbootDataJpaAuditApplication class as shown below. Using CommandLineRunner we will first save few records and then later fetch those records.

When you run your application, you should see output similar to the following

Auditing the Author of Changes

We will add @CreatedBy, @LastModifiedBy to capture the user who created or modified the entity. These fields will be automatically populated if Spring Security is available in the project path. Alternatively we will implement our own AuditorAware in order to inform Spring who is the current auditor.

In AuditorAwareImpl we can write our custom business logic for fetching current auditor. I have hardcoded for simplicity reasons.

NOTE: In order to keep our application code cleaner, I moved audit configuration code to separate class. Final code looks like below.

AuditConfiguration.java

AuditorAwareImpl.java

BookEntity.java

On re running the application, you should see output similar to the following

Bonus Point:

we can modify createdDate and modifiedDate field types to long to store such information in Long instead of Date which may be handy in some cases. sample log looks like below.

Conclusion:

If you would like to refer to the full code, do check https://github.com/projectk-user1/Springboot-learning.git

Please do refer my other articles on Spring boot.

Feel free to provide your suggestions in comments section

--

--