8 Replies Latest reply on Dec 13, 2010 9:08 AM by anonimo

    Saving only modified entities

    anonimo

      Hi,

       

      I am experiencing a behavour in Hibernate that seems to be not very convenient for me.

       

      Say I have an Employee entity, which is annotated with @Audited. The Employee entity holds a one-to-one relationship with a Desk entity. The thing is, whenever I persist this relationship, without modifying any of the attributes of Employee and Desk, Envers creates new revisions of those entities.

       

      Is there a way of telling Envers not to create new revisions of an audited entity if none of the attributes have been modified? This is causing lots of duplicated records in my database as you can imagine.

       

      Thanks

      G.

        • 1. Re: Saving only modified entities
          adamw

          Hmm that's very weird ... can you try to set the org.hibernate.envers.revision_on_collection_change property to false?

          Also take a look at http://opensource.atlassian.com/projects/hibernate/browse/HHH-5756 - maybe it's something similar?

           

          Adam

          • 2. Re: Saving only modified entities
            anonimo

            Thanks for the reply Adam. I think this has nothing to do with collections though. Imagine there is no Desk entity, only an Employee entity. If I save the Employee without modifying any of the attributes (none of these attributes are relationships to other entities but only Strings, Integers, etc) Envers keeps creating new revisions of this entity although nothing has really changed.

            • 3. Re: Saving only modified entities
              adamw

              That's really weird, a new revision should be saved only if there are some modifications.

              Could you maybe prepare a failing test case for this?

               

              Thanks,

              Adam

              • 4. Re: Saving only modified entities
                anonimo

                I attach the use case that is causing the problem. In this use case, I save a new entity, and then in a different session, I save another entity which has exactly the same attributes as the original one. This causes a new revision being created.

                 

                However, if when in the second session, right after saving the first entity, I first load it in the persistent context and then modify the values and persist it, a second revision is not created.

                 

                In both cases the modified entity has the same values (nothing changes) but one of them creates a new revision and the other doesn't.

                 

                Thanks

                • 5. Re: Saving only modified entities
                  adamw

                  But in the "regular" table, do you get two rows? The entities may differ only in their IDs...

                   

                  Adam

                  • 6. Re: Saving only modified entities
                    anonimo

                    It is exactly the same record, no columns are changed.

                    • 7. Re: Saving only modified entities
                      konyshev

                      Is there any progress on this issue? Got the same problem, revisions being created even there is no data changed.

                      • 8. Re: Saving only modified entities
                        anonimo

                        Hi Sergey. I finally managed to get this working by retrieving the entity I want to modify first. If I then update it while it is in the persistent context, no revisions are created if it hasn't changed. However, If I try to saveOrUpdate a non-persistent entity whose ID already exists, a new revision is created.