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
abstractclass (e.g.,BaseEntity) annotated with@MappedSuperclass - in this
abstractclass, define a field namedcreatedand annotate it with the built-in@CreationTimestampannotation - in this
abstractclass, define a field namedlastModifiedand annotate it with the built-in@UpdateTimestampannotation - in this
abstractclass, define a field namedcreatedByand annotate it with the@CreatedByannotation - in this
abstractclass, define a field namedlastModifiedByand annotate it with the@ModifiedByannotation - implement the
@CreatedByannotation viaAnnotationValueGeneration - implement the
@ModifiedByannotation viaAnnotationValueGeneration - every entity that want to take advantage of
created,createdBy,lastModifiedandlastModifiedBywill extend theBaseEntity - store the date-time in UTC

