jeudi 20 juin 2019

Compare two pojo and output difference to another pojo

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.





Aucun commentaire:

Enregistrer un commentaire