审核实体JPA中的更改 [英] Auditing Changes in Entity 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.
要启用审核,
- 应在配置类上添加
@EnableJpaAuditing
实体上的 -
@Audited
和@EntityListeners(AuditingEntityListener.class)
-
AuditorAware<T>
提供用户名 - 儿童实体应使用
@Audited
进行注释
- should add
@EnableJpaAuditing
on configuration class @Audited
and@EntityListeners(AuditingEntityListener.class)
on entitiesAuditorAware<T>
to provide the username- 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
-
{ENTITY_NAME}_AUD
//可以覆盖审核表名称的前缀和后缀 -
REVINFO
{ENTITY_NAME}_AUD
// can override the prefix and suffix of audit table nameREVINFO
下面是休眠和春季审核一对多关系的示例
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屋!