Compare two pojo and output difference to another pojo

一世执手 提交于 2019-12-24 15:04:02

问题


I have a pojo class as follows.

public class Person {
    private String name;
    private Address address;
    public String getName() { ... }
    public Address getAddress() { ... } //Address bean
    public void setName() { ... }
    public void setAddress() { ... }
}
  • Get data from DB (json converted to above POJO)
  • Show it to user
  • User changes his Address
  • Saving back to DB

This is what currently happening. Now I am trying to make a delta of user made changes Vs database changes. (which is address alone)

I need to know what user has changed. This is for making an audit log.

In this case it's address. That new address should be put into the new Person-POJO class (with all other setters as NULL).

So that I would get

address {
 -- new address
},
name {
 -- NULL //or no need to have "name" as address only changed
}

What would be the best method to achieve this ?

I know there are few libraries with with we can compare beans with some comparators. But it will just compare, will not provide the changed part alone. If there is any, please provide a link.

I got an idea from https://stackoverflow.com/a/472637/2086966 answer. Trying to do as this method.

来源:https://stackoverflow.com/questions/56683389/compare-two-pojo-and-output-difference-to-another-pojo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!