In all our entity classes, we have these columns.
CREATED_AT, CREATED_BY, UPDATED_AT, UPDATED_BY.
We are maintaining these four metadata columns which will tell us, when a record is created, by whom and when it is updated and by whom.
With the help of Spring Data JPA, we can update these metadata columns automatically, which means we can give this responsibility to the Spring Data JPA.
Since Spring Data JPA is taking care of all SQL interactions and database interactions, we can hand over this responsibility of updating these four metadata columns. And Spring Data JPA also supports this auditing feature.
Auditing by Spring Data JPA
In order to achieve this auditing by Spring Data JPA, first we need to go to the entity class where we have these four metadata columns.
Use the annotations :
@CreatedData@CreatedBy
@LastModifiedData@LastModifiedBy
These are the 4 annotations supported by the Spring Data framework itself.
With these annotations we are telling the spring framework, whenever you see these fields inside a table or an entity, please make sure you are updating the date time based upon when the record is being updated or inserted. The same applies for the last modified date as well.
With the help of @CreatedBy and @LastModifiedBy, we are telling to the Spring Data JPA framework like who is trying to create or update the records inside the database table.
@MappedSuperclass
@Getter
@Setter
@ToString
public class BaseEntity {
@Column(updatable = false)
@CreatedDate
private LocalDateTime createdAt;
@Column(updatable = false)
@CreatedBy
private String createdBy;
@Column(insertable = false)
@LastModifiedDate
private LocalDateTime updatedAt;
@Column(insertable = false)
@LastModifiedBy
private String updatedBy;
}
Problem ?
My Spring Data JPA framework can get that date-time from the server or from the local system.
How about who is creating or who is updating that data? It does not have any clue.
Let me create a new package with the name audit. Inside this audit package, I’m going to create a new class with the name AuditAwareImpl.
public class AuditAwareImpl {
}
This is the empty class for now.
This class should implement an AuditorAware interface.
Implementing AuditorAware interface
Empty implementation –
Since our createdBy and updatedBy are of type String, we need to make sure we are mentioning this String.
import org.springframework.data.domain.AuditorAware;
import java.util.Optional;
public class AuditAwareImpl implements AuditorAware<String> {
/**
* @return
*/
@Override
public Optional<String> getCurrentAuditor() {
return Optional.empty();
}
}
For now, I’m going to return an hardcoded value which is ACCOUNTS_MS.
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component("auditAwareImpl")
public class AuditAwareImpl implements AuditorAware<String> {
/**
* @return
*/
@Override
public Optional<String> getCurrentAuditor() {
return Optional.of("ACCOUNTS_MS");
}
}
This is the value “ACCOUNTS_MS”, that I want to be considered by the Spring Data JPA framework whenever it is trying to populate the columns createdBy and updatedBy.
Linking the Entity class with Auditing class
@EntityListeners
Once we define this component, we need to go to the BaseEntity class where we have the metadata columns and here we need to mention an annotation which is @EntityListeners.
And to this annotation I need to pass a class name which is AuditingEntityListener.class. This is a class present inside the Spring Data framework.
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Getter
@Setter
@ToString
public class BaseEntity {
@Column(updatable = false)
@CreatedDate
private LocalDateTime createdAt;
@Column(updatable = false)
@CreatedBy
private String createdBy;
@Column(insertable = false)
@LastModifiedDate
private LocalDateTime updatedAt;
@Column(insertable = false)
@LastModifiedBy
private String updatedBy;
}
Now we have established all the details in order to perform the auditing by the Spring Data framework.
Activate Auditing in Spring framework
@EnableJpaAuditing annotation
In order to perform the auditing by the Spring Data framework, as a last step, we need to activate this feature provided by the Spring Data Framework.
For the same, we need to go to the main class, which is AccountsApplication after the @SpringBootApplication, we need to mention an annotation which is @EnableJpaAuditing.
@SpringBootApplication
@EnableJpaAuditing
public class AccountsApplication {
public static void main(String[] args) {
SpringApplication.run(AccountsApplication.class, args);
}
}
→ Using this @EnableAuditing annotation, we need to invoke the param which is auditorAwareRef.
To this auditorAwareRef, we need to pass the bean name of the AuditoAwareImpl.
@SpringBootApplication
@EnableJpaAuditing(auditorAwareRef = "auditAwareImpl")
public class AccountsApplication {
public static void main(String[] args) {
SpringApplication.run(AccountsApplication.class, args);
}
}
Build and Deploy
The application is started at port number 8080.
Using Postman, you can invoke the REST APIs.
Check the details in the database and observe –
After you update the data, you can see :
So we right now gave the responsibility of auditing to the spring data framework. This is great and this is one of the standards that you can also follow inside your REST APIs and microservices.