8 Replies Latest reply on Jul 29, 2009 5:43 PM by shay1680

    display only changed fields

    shay1680 Newbie


      I need to display an audit report , that will show only the fields that were changed in each entity in each transaction.

      what is the best way to achieve that?


        • 1. Re: display only changed fields
          Adam Warski Master


          currently the only way to do it is to get all revisions of an entity and compare each with the previous. Each revision contains at least one modified field, so you'd have to load all revisions anyway ...


          • 2. Re: display only changed fields
            shay1680 Newbie

            Thanks Adam,

            Any suggestions as to the most effective way for comparing 2 revisions of the same entity.
            i came up with 2 ways neither is very efficient
            1) use IF statements for each field
            2) use reflection to iterate over all fields


            • 3. Re: display only changed fields
              Varun Mehta Newbie

              You might want to try this;

              We too are looking to implement the same feature and might visit the same route.

              • 4. Re: display only changed fields
                shay1680 Newbie

                thanks varun,

                my report needs to show the following format:
                user A changed product name from B to C

                as it currently stands , i might have to remove envers and implement my auditing via the accessors.

                the approach that envers takes is great if you want to rollback changes , but doesn't work well for my reporting purposes.


                • 5. Re: display only changed fields
                  Adam Warski Master

                  well, approach 1) is faster but inpractical for more entities, 2) is more generic but a bit slower ;)

                  Why can't you use Envers for this purpose? If you query for revisions of an entity, you can compare each with the previous one using one of the methods above, and get the username from the revision entity.


                  • 6. Re: display only changed fields
                    shay1680 Newbie


                    lets use a sample scenario:

                    a product in an online auction system, with the following fields : name , description . product table is also linked to price-bid table which has fields: price, user . it is also linked to comments table , with fields: comment, user

                    all the fields can change at any time by numerous users.

                    you want to print a verbose report of who changed what and when , each line should be descriptive i.e.
                    1) user x , added comment
                    2) user y upped his bid limit from a to b


                    i might be wrong , but
                    but using reflection to generate this report will result in an enormous number of permutations, not to mention the fact that to get verbose comments you would still need if-else statements all for e each comparison.

                    what do you think?


                    • 7. Re: display only changed fields
                      Varun Mehta Newbie

                      Something at an abstract level

                      Uses reflection, but can be optimized with some check points. Using hashcode, instead of comparing field by field values, just compare the field hashcodes.


                      class UserComments {
                       int commentId;
                       User user;
                       String description;
                       BigDecimal price;
                       // their getter/setters etc...

                      So you get 2 versions of the same class, If you run a hashcode on both the objects and they are same, you have the same object, run hashcodes on all the fields and register what changed.

                      If your nested objects (User in UserComments) implements E/HC properly on calling hashcode on the object itself will tell you if the user changed.

                      You can capture only the fields which changed, and create a datastructure which holds this information.


                      On a different tangent, use Castor or JAXB and convert the objects to XML and then use a text file Diff API to show the difference, you can find a lot of stuff on the text diff API (SVN style)

                      • 8. Re: display only changed fields
                        shay1680 Newbie

                        thanks much
                        ill try this approach and see how well it scales for a large number of classes