Auditing Changes in Entity JPA

前端 未结 1 1046
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 02:34

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 the

相关标签:
1条回答
  • 2021-01-07 03:12

    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

    Example

    @Bean
    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
    2. REVINFO

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

    UserInfo.java

    @Audited
    @Entity
    @EntityListeners(AuditingEntityListener.class)
    public class UserInfo extends AuditInfo {
    
        @Id
        @GeneratedValue
        private Long id;
    
        @Column
        private String name;
    
        @OneToMany(mappedBy = "userInfo", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
        private List<UserAddress> addresses;
    
    }
    

    UserAddress.java

    @Entity
    @Audited
    @EntityListeners(AuditingEntityListener.class)
    public class UserAddress extends AuditInfo {
    
        @Id
        @GeneratedValue
        private Long addressId;
    
        @ManyToOne
        @JoinColumn(name = "id", nullable = false)
        private UserInfo userInfo;
    
        @Column
        private Long no;
    
        @Column
        private String street;
    
    }
    

    AuditInfo.java

    @EntityListeners(AuditingEntityListener.class)
    @MappedSuperclass
    public abstract class AuditInfo {
    
        @CreatedBy
        private String createdBy;
    
        @LastModifiedBy
        private String updatedBy;
    
        @CreatedDate
        private LocalDateTime createdOn;
    
        @LastModifiedDate
        private LocalDateTime updatedOn;
    
    }
    
    0 讨论(0)
提交回复
热议问题