Skip to content

Latest commit

 

History

History

HibernateSpringBootAudit

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

How To Setup Spring Data JPA Auditing

Description: Auditing is useful for maintaining history records. This can later help us in tracking user activities.

Key points:

  • create an abstract base entity (e.g., BaseEntity) and annotate it with @MappedSuperclass and @EntityListeners({AuditingEntityListener.class})
  • in this base entity, add the following fields that will be automatically persisted:
          - @CreatedDate protected LocalDateTime created;
          - @LastModifiedDate protected LocalDateTime lastModified;
          - @CreatedBy protected U createdBy;
          - @LastModifiedBy protected U lastModifiedBy;
  • enable auditing via @EnableJpaAuditing(auditorAwareRef = "auditorAware")
  • provide an implementation for AuditorAware (this is needed for persisting the user that performed the modification; use Spring Security to return the currently logged-in user)
  • expose this implementation via @Bean
  • entites that should be audited should extend the base entity
  • store the date-time in database in UTC

If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.