3 Replies Latest reply on Jun 2, 2011 6:23 AM by ciaran.hanley

    Entity Audited Without Modifications

    ciaran.hanley

      I noticed that some of our audit tables are filling rapidly without any modifications being tracked.

       

      A quick example:

      1. There is a simple @Audited entity, say Person, which has a property "name".

      2. A Person entity is loaded from database and the name property value is null.

      3. The Person property "name" is displayed on Edit Person JSP for modification. A user chooses Save on the JSP without making any modifications.

      4. As no value is provided for name on JSP, the name is populated with empty string.

      5. Hibernate treats this as an Entity change and issues an EntityUpdateAction

      6. PostUpdateEventListener is called and audit entry is created.

      7. Everytime the Edit Person JSP opens and is Saved  an Audit record is created regardless of whether data changed. There are no changes in the AUD rows in DB for any of the entries created.

       

      This might be more of a Hibernate issue as empty strings are loaded as null from Oracle DB. I thought users of Envers must have encountered the same problem and might have a solution.

       

      Thanks,

      Ciaran

        • 1. Re: Entity Audited Without Modifications
          adamw

          So the field is modified in Java (null -> ""), but is not modified from an Oracle point of view?


          As a work-around maybe you can create an @PreUpdate, @PreInsert event to set the empty-string names to nulls.

           

          As a proper fix, I supose we should make a special-case for Oracle? Or maybe this is already somehow encapsulated in the Hibernate Dialect object, and Envers just needs to use it. Anyway, thanks for reporting the issue and please create a JIRA for that. And if you would like to contribute a fix ...

           

          Adam

          • 2. Re: Entity Audited Without Modifications
            ciaran.hanley

            Hi Adam,

             

            Yes that is correct. The entity is loaded and the property is set from null to empty string. The entity is then persisted. Next time the entity is loaded the field will have value null as Oracle stores empty string as null.

             

            As the object has changed state (null -> ""), the dirty flag must be set on the entity and Hibernate adds an update to the action queue which in turn raises the PostUpdateEvent to the Envers listener.

             

            I had a quick look at the Oracle Dialect and could not see anything of use there. It seems to be more of an issue with Hibernate not being able to recognize that null -> "" should not result in an update if an Oracle Dialect is being used, rather than something in Envers. Would you agree?

             

            Thanks,

            Ciaran

            • 3. Re: Entity Audited Without Modifications
              ciaran.hanley

              After some further googling it seems using a custom user type has been previously recommended

               

              https://forum.hibernate.org/viewtopic.php?f=1&t=965579&view=next

               

              It seems like a lot of work to implement such a solution as we have hundreds of entities in the application.