-1

Am using spring web and hibernate . I need to compare two complex DTOs of the same type one received via POST request body and the other got from the DB . There are at least several hundred properties and list of child objects in this DTO .. I have to compare these two DTOs in order to check if any of the fields modified excluding a few properties (20 properties ) .. can someone give me some suggestions on how this can be done easily ..

Jenny
  • 77
  • 1
  • 6
  • what's the structure of the DTO? is it flat? Nested objects? If its flat you might be able to put the properties in maps and then remove your 20 properties and diff the two maps. – trichner Nov 13 '18 at 18:04
  • Its not flat .. it has nested objects .. – Jenny Nov 13 '18 at 18:29
  • You can consider using Assertj library. See this answer - https://stackoverflow.com/a/64520873/1385441 – Ram Patra May 05 '22 at 21:56

4 Answers4

1

I'd recommend to use Decorator design pattern instead of creating Utils classes or setting parameters to null and restoring then after comparison.

Here is an implement suggestion and in your case you only need to override the equals()

TOvidiu
  • 1,036
  • 11
  • 15
  • I don't understand how does this pattern help in comparing two complex DTOs ? I need to compare the two DTO s to see if the fields and objects haven't been modified , excluding a few properties .... can u elaborate how is this design pattern going to help? – Jenny Nov 13 '18 at 18:35
  • Wrapping each DTO into a decorator class and override the equals(). You may need to make a costume constructor if the DTO are different and map them into one of those 2 DTO types (if are different or you could look up for orika mapping). – TOvidiu Nov 13 '18 at 18:40
0

It may works if you create a Class method that recieves two objects of itself and compare them by checking certain properties.

class MyClass {
 (...)
   public static int compare (MyClass m1, MyClass m2){
        if (m1.name == m2.name){ return 1; }
        return 0;
   }
}

You can also check the 'Comparable' interface (this is a better solution): https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

Hope it helps :)

Juanju
  • 101
  • 3
0

If I have a variable x then I do something and ask "did x change?", you can't answer that question without knowing the initial and final value of x. Therefore if you want to check if certain fields changed then you must read and compare those fields initial and final values.

You can add some efficiency by caching hash codes of the objects, and if the hash codes are different then you know the objects are different, but calculating the hash code requires reading all the fields anyway.

I'd say just make the equals method, most IDEs can auto generate the bulk of it anyway.

xtratic
  • 4,600
  • 2
  • 14
  • 32
  • There is no equals method .. comparing each field is not practical because there are 100s fields .. and there are nested objected within as well – Jenny Nov 13 '18 at 18:31
  • Practical or not, the only way to determine equality between values is to read and compare the values. Even if you cached hash codes for these objects, hashes could collide and you would still have to check all the fields you are interested in to test for equality. But hash codes at least could quickly tell you if the objects are not equal. – xtratic Nov 13 '18 at 18:42
0

Use Apache Commons-Lang

EqualsBuilder.reflectionEquals(expected, argument, "someField");
KevinC
  • 306
  • 2
  • 6