java审计属性更改Spring MVC+JPA
我有一个班级客户。我希望能够审核这个类(不是整个类——只是它的属性)属性的更改
public class Client {
private Long id;
private String firstName;
private String lastName;
private String email;
private String mobileNumber;
private Branch companyBranch;
实际上,使用@Audited注释审计整个实体非常容易
但我想用我的类结构来审核这些变化
下面是我想要的结果:
public class Action {
private String fieldName;
private String oldValue;
private String newValue;
private String action;
private Long modifiedBy;
private Date changeDate;
private Long clientID;
结果应该是这样的:
fieldName+由“modifiedBy”将“clientID+”的“+oldValue+”改为“+newValue+”
- 乔治把比尔·盖茨的手机号码从555改为999李>
我这样做的原因是,我需要将这些更改存储到Action table下的DB中,因为我将审核来自不同实体的属性,我希望将它们存储在一起,然后在需要时能够获取它们
我该怎么做
谢谢
# 1 楼答案
我希望您应该用审计属性覆盖实体的equals方法。 在DAO中,您只需使用您在实体内部创建的equals方法将实体的旧实例与新实例进行比较
你将能够识别这是否是可审计的
# 2 楼答案
AOP absulately是针对您的案例的解决方案,我用Spring AOP实现了类似的案例,以保留实体修订。这个解决方案的一个要点是需要在切入点附近使用
另一个解决方案是使用
org.hibernate.Interceptor
,org.hibernate.EmptyInterceptor
应该是适当的扩展,我编写了一些简单的代码来模拟它(以客户机代码为例):实现
Interceptor
注册inceptor,我使用的是spring boot,所以只需将它添加到
application.properties
这是测试
将得到如下输出:
所以假设它可以被
Action
对象替换这是一个开源项目JaVers - object auditing and diff framework for Java
你可以看看这个项目