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 namedcreated
and annotate it with the built-in@CreationTimestamp
annotation - in this
abstract
class, define a field namedlastModified
and annotate it with the built-in@UpdateTimestamp
annotation - in this
abstract
class, define a field namedcreatedBy
and annotate it with the@CreatedBy
annotation - in this
abstract
class, define a field namedlastModifiedBy
and annotate it with the@ModifiedBy
annotation - implement the
@CreatedBy
annotation viaAnnotationValueGeneration
- implement the
@ModifiedBy
annotation viaAnnotationValueGeneration
- every entity that want to take advantage of
created
,createdBy
,lastModified
andlastModifiedBy
will extend theBaseEntity
- store the date-time in UTC