审核实体JPA中的更改 [英] Auditing Changes in Entity JPA

查看:143
本文介绍了审核实体JPA中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Spring MVC + JPA应用程序.

I have Spring MVC + JPA applications.

我的应用程序中有几个实体在不断变化.我希望能够审核此更改.我发现有一个@Audited批注可以跟踪对某些字段或整个Entity的更改.我想知道是否有任何方法可以配置此跟踪选项-我希望能够跟踪已更改的内容以及更改的人.还可以对SQL 1个表中的多个实体进行更改吗?还可以跟踪实体中@OneToMany个字段的变化吗?

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?

谢谢

推荐答案

是的,您可以跟踪所做的更改,更新的用户和时间戳.

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

Hibernate提供@Audited批注以维护实体版本.

Hibernate provides @Audited annotation to maintain entity version.

Spring提供了@CreatedBy@LastModifiedBy@CreatedDate@LastModifiedDate批注,其中您需要提供使用AuditorAware bean更新的用户名.

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

要启用审核,

  1. 应在配置类上添加@EnableJpaAuditing
  2. 实体上的
  3. @Audited@EntityListeners(AuditingEntityListener.class)
  4. AuditorAware<T>提供用户名
  5. 儿童实体应使用@Audited
  6. 进行注释
  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

示例

@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//可以覆盖审核表名称的前缀和后缀
  2. REVINFO
  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

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

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

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;

}

这篇关于审核实体JPA中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆