4

I want to compare two object i.e two database rows field by field. e.g. Object1[name="ABC", age=29, email="abc@amail.com"] and Object2[name="XYZ", age=29, email="xyz@amail.com"]

suppose I want to compare these two object and I want output like this

[{
 "fieldName" : "email",
 "OldObjectValue" : "abc@amail.com",
 "NewObjectValue" : "xyz@amail.com"
},
{
 "fieldName" : "name",
 "OldObjectValue" : "ABC",
 "NewObjectValue" : "XYZ"
}]

Here age is same so age field is not present in output.

If this is possible by doing generic method using reflection please provide some code. because I have not worked on reflection yet. Please help.

Rohit K
  • 93
  • 1
  • 1
  • 7
  • If you want this implemented in your business logic, then it is probably going to happen in your Java code, in which case the Hibernate tag may not be appropriate. – Tim Biegeleisen Aug 25 '15 at 05:25
  • @bitstrider I tried manually comparing of each field by using equals and == – Rohit K Aug 25 '15 at 05:33
  • what are you looking to do with the output, store it in an object? – strider Aug 25 '15 at 05:38
  • It is best that you implement it for your business logic. If you want something more generic reflections are needed – DanielM Aug 25 '15 at 05:39
  • @bitstrider yes..I want to store it in object and send back in response – Rohit K Aug 25 '15 at 05:42
  • If this is possible by doing generic method using reflection please provide some code. because I have not worked on reflection yet – Rohit K Aug 25 '15 at 06:56
  • @Rohit K why do you suggest such powerfull tool for such trival job? It is not said that he has to compare two different classes. Libs will do this tasks via reflections an result will be slow. – T.G Aug 25 '15 at 07:02
  • It is like digging in a garden to plant a flower with a bulldozer. Using a lib to do such a customized thing? Rather, set up a helper method! – javadev Aug 25 '15 at 07:10

1 Answers1

15

According to your requirement you can do this as follow.

you can take two database rows to two objects. Eg: SampleObject

public class SampleObject {

private String name;
private int age;
private String email;   

public SampleObject(String name, int age, String email) {
    this.name = name;
    this.age = age;
    this.email = email;
}
.
.

I imagine your results will be an object too. Eg : ResultObject

public class ResultObject {

private String fieldName;
private String OldObjectValue;
private String NewObjectValue;
.
.

You can just define a compareField kind of method in SampleObject

public List<ResultObject> compareFields(SampleObject object) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
    List<ResultObject> resultList = new ArrayList<ResultObject>();      
    Field[] fields = this.getClass().getDeclaredFields();

    for(Field field : fields){
        if(!field.get(this).equals(field.get(object))){
            ResultObject resultObject = new ResultObject();
            resultObject.setFieldName(field.getName());
            resultObject.setOldObjectValue(field.get(this).toString());
            resultObject.setNewObjectValue(field.get(object).toString());
            resultList.add(resultObject);
        }
    }
    return resultList;
}

Then you can make it work.

SampleObject object1 = new SampleObject("ABC", 29, "abc@amail.com");
    SampleObject object2 = new SampleObject("XYZ", 29, "xyz@amail.com");

    List<ResultObject> resultList = object1.compareFields(object2);

Thanks

isurujay
  • 1,386
  • 10
  • 15
  • Thanks for help...If I have 20 fields in an object then this code will not help as I have to compare each field...I need generic solution for this – Rohit K Aug 25 '15 at 12:22
  • Yes. Only issue for the generic solution is getting the field name. But we can use java Reflection for get the field name too then. If so we can write this in generic way. – isurujay Aug 25 '15 at 12:35
  • 2
    Answer was updated. Used java reflections for a generic solution. Thanks – isurujay Aug 25 '15 at 13:14