find out the differences between two java beans for version tracking

后端 未结 4 1062
耶瑟儿~
耶瑟儿~ 2020-11-30 06:13

say i have a java bean/an entity with 100 fields (inherited or not it is not relevant in this case). After update operations - in a transaction, i want to determine which fi

4条回答
  •  -上瘾入骨i
    2020-11-30 06:19

    Hey look at Javers it's exactly what you need - objects auditing and diff framework . With Javers you can persist changes done on your domain objects with a single javers.commit() call after every update. When you persist some changes you can easily read them by javers.getChangeHistory, e.g.

    public static void main(String... args) {
        //get Javers instance
        Javers javers = JaversBuilder.javers().build();
    
        //create java bean
        User user = new User(1, "John");
    
        //commit current state
        javers.commit("author", user);
    
        //update operation
        user.setUserName("David");
    
        //commit change
        javers.commit("author", user);
    
        //read 100 last changes
        List changes = javers.getChangeHistory(instanceId(1, User.class), 100);
    
        //print change log
        System.out.printf(javers.processChangeList(changes, new SimpleTextChangeLog()));
    }
    

    and the output is:

    commit 2.0, author:author, 2015-01-07 23:00:10
      changed object: org.javers.demo.User/1
        value changed on 'userName' property: 'John' -> 'David'
    commit 1.0, author:author, 2015-01-07 23:00:10
        new object: 'org.javers.demo.User/1
    

提交回复
热议问题