Check if two objects are equal excluding a few properties












-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 ..










share|improve this question























  • 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
















-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 ..










share|improve this question























  • 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














-1












-1








-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 ..










share|improve this question














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 ..







java spring






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 18:01









JennyJenny

42




42













  • 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



















  • 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

















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







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





Its not flat .. it has nested objects ..

– Jenny
Nov 13 '18 at 18:29












3 Answers
3






active

oldest

votes


















0














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()






share|improve this answer
























  • 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).

    – Skenia
    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 :)






share|improve this answer

































    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.






    share|improve this answer


























    • 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













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286987%2fcheck-if-two-objects-are-equal-excluding-a-few-properties%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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()






    share|improve this answer
























    • 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).

      – Skenia
      Nov 13 '18 at 18:40
















    0














    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()






    share|improve this answer
























    • 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).

      – Skenia
      Nov 13 '18 at 18:40














    0












    0








    0







    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()






    share|improve this answer













    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()







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 18:22









    SkeniaSkenia

    16817




    16817













    • 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).

      – Skenia
      Nov 13 '18 at 18:40



















    • 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).

      – Skenia
      Nov 13 '18 at 18:40

















    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





    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).

    – Skenia
    Nov 13 '18 at 18:40





    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).

    – Skenia
    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 :)






    share|improve this answer






























      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 :)






      share|improve this answer




























        0












        0








        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 :)






        share|improve this answer















        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 :)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 18:24

























        answered Nov 13 '18 at 18:13









        Juan Julián Cea MoranJuan Julián Cea Moran

        463




        463























            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.






            share|improve this answer


























            • 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














            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.






            share|improve this answer


























            • 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












            0








            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 19:25

























            answered Nov 13 '18 at 18:13









            xtraticxtratic

            2,4691822




            2,4691822













            • 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





















            • 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



















            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





            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







            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




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286987%2fcheck-if-two-objects-are-equal-excluding-a-few-properties%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation