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.
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.
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.
On re running the application, you should see output similar to the following
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.
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.
Spring Boot Data JPA — Part II (Pagination and Sorting)
In Previous post I explained how to get started with Spring boot Data JPA. In this post we will discuss how to use…
Getting started with Spring Boot Data JPA
In Previous post I explained how to get started with Spring boot. In this post we will use Spring Boot along with…
Spring Boot REST Web Services Validation
Validation is a common requirement in all the services. When developing a REST API, it’s important to validate request…
Spring Boot RESTfull web service
In Previous post I explained how to get started with Spring boot. In this post we will go through how to create…
Feel free to provide your suggestions in comments section