有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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中,因为我将审核来自不同实体的属性,我希望将它们存储在一起,然后在需要时能够获取它们

我该怎么做

谢谢


共 (2) 个答案

  1. # 1 楼答案

    我希望您应该用审计属性覆盖实体的equals方法。 在DAO中,您只需使用您在实体内部创建的equals方法将实体的旧实例与新实例进行比较

    你将能够识别这是否是可审计的

  2. # 2 楼答案

    AOP absulately是针对您的案例的解决方案,我用Spring AOP实现了类似的案例,以保留实体修订。这个解决方案的一个要点是需要在切入点附近使用

    另一个解决方案是使用org.hibernate.Interceptororg.hibernate.EmptyInterceptor应该是适当的扩展,我编写了一些简单的代码来模拟它(以客户机代码为例):

    @Entity
    public class Client {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        private String firstName;
        private String lastName;
        private String email;
        private String mobileNumber;
        // getter and setter
    }
    

    实现Interceptor

    public class StateChangeInterceptor extends EmptyInterceptor {
        @Override
        public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
            if (entity instanceof Client) {
                for (int i = 0; i < propertyNames.length; i++) {
                    if (currentState[i] == null && previousState[i] == null) {
                        return false;
                    } else {
                        if (!currentState[i].equals(previousState[i])) {
                            System.out.println(propertyNames[i] + " was changed from " + previousState[i] + " to " + currentState[i] + " for " + id);
                        }
                    }
    
                }
            }
    
            return true;
        }
    
        @Override
        public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
            return super.onSave(entity, id, state, propertyNames, types);
        }
    }
    

    注册inceptor,我使用的是spring boot,所以只需将它添加到application.properties

    spring.jpa.properties.hibernate.ejb.interceptor=io.cloudhuang.jpa.StateChangeInterceptor
    

    这是测试

    @Test
    public void testStateChange() {
        Client client = new Client();
        client.setFirstName("Liping");
        client.setLastName("Huang");
    
        entityManager.persist(client);
        entityManager.flush();
    
        client.setEmail("test@example.com");
        entityManager.persist(client);
        entityManager.flush();
    }
    

    将得到如下输出:

    email was changed from null to test@example.com for 1
    

    所以假设它可以被Action对象替换

    这是一个开源项目JaVers - object auditing and diff framework for Java

    JaVers is the lightweight Java library for auditing changes in your data.

    你可以看看这个项目