Skip to content

Latest commit

 

History

History

HibernateSpringBootTimestampGeneration

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

How To Add created, createdBy, lastModified And lastModifiedBy In Entities Via Hibernate

Note: The same thing can be obtained via Spring Data JPA auditing as here.

Description: This application is an example of adding in an entity the fields, created, createdBy, lastModified and lastModifiedBy via Hibernate support. These fields will be automatically generated/populated.

Key points:

  • write an abstract class (e.g., BaseEntity) annotated with @MappedSuperclass
  • in this abstract class, define a field named created and annotate it with the built-in @CreationTimestamp annotation
  • in this abstract class, define a field named lastModified and annotate it with the built-in @UpdateTimestamp annotation
  • in this abstract class, define a field named createdBy and annotate it with the @CreatedBy annotation
  • in this abstract class, define a field named lastModifiedBy and annotate it with the @ModifiedBy annotation
  • implement the @CreatedBy annotation via AnnotationValueGeneration
  • implement the @ModifiedBy annotation via AnnotationValueGeneration
  • every entity that want to take advantage of created, createdBy, lastModified and lastModifiedBy will extend the BaseEntity
  • store the date-time 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.