Data Auditing in Spring Data JPA
Many systems require auditing and tracking the change in the persistence layer. It is really helpful to investigate problems related to the data. Data auditing typically answers these questions: when was it created, who was create the record, when was it created, who modified the record last and when was it modified last.
Spring provides four annotations in the spring-data-commons module for auditing features.
@CreatedByrepresents the principal that created the entity containing the field.
@CreatedDate represents the date the entity containing the field was created.
@LastModifiedBy represents the principal that recently modified the entity containing the field.
@LastModifiedDaterepresents the date the entity containing the field was recently modified.
Besides the four mentioned annotations, Spring also provides @EnableJpaAuditing that aims enable auditing feature.
In order to explain how the auditing feature works in Spring Data JPA, we should be known about an important class that listens to the change of the entity. It is AuditingEntityListener class. The AuditingEntityListener is one of the key factors of auditing features in Spring. Its responsibility is to listen to an update or a creation of an entity then perform necessary tasks to fill auditing information to the entity based on the annotated annotation of the entity. This class relies on the JPA Entity lifecycle event.
Comments
Post a Comment