Purmarili Purmarili - 1 year ago 209
Java Question

Auditing Changes in Entity JPA

I have Spring MVC + JPA applications.
I have several entities in application which Are constantly changing. I want to be able to audit this changes. I found that there is an @audited annotation that track changes to certain fields or whole Entity. I want to know if there any way to configure this track options - I want to be able to track what was changed and who changed it.Also is it possible to have changes from several Entities in 1 Table in SQL? Also is it possible to track changes of - @OneToMany Fields in Entity?


Answer Source

Yes, you can keep track the changes made, updated user and time-stamp.

Hibernate provides @Audited annotation to maintain entity version.

Spring provides @CreatedBy @LastModifiedBy @CreatedDate and @LastModifiedDate annotations, among these you need to provide the user name who updates using AuditorAware bean.

To enable auditing,

  1. should add @EnableJpaAuditing on configuration class
  2. @Audited and @EntityListeners(AuditingEntityListener.class) on entities
  3. AuditorAware<T> to provide the username
  4. Children entities should be annotated with @Audited


public AuditorAware<String> createAuditorProvider() {
    return () -> "username"; // should be from context/session

For each entities an additional table will be created to maintain version

  1. {ENTITY_NAME}_AUD // can override the prefix and suffix of audit table name

below is an example of one-to-many relationship with hibernate and spring auditing


public class UserInfo extends AuditInfo {

    private Long id;

    private String name;

    @OneToMany(mappedBy = "userInfo", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<UserAddress> addresses;



public class UserAddress extends AuditInfo {

    private Long addressId;

    @JoinColumn(name = "id", nullable = false)
    private UserInfo userInfo;

    private Long no;

    private String street;



public abstract class AuditInfo {

    private String createdBy;

    private String updatedBy;

    private LocalDateTime createdOn;

    private LocalDateTime updatedOn;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download