4 Replies Latest reply on Nov 12, 2010 5:07 AM by adamw

    Can I audit only a foreign key and not the reffered entity?

    florin13

      Hi,

       

         I have 2 entities: Person and Name and a unidirectional one-to-one relationship from Person to Name. The Person object has a reference to a Name object. In the DB the Person table has a FK:Name_Id associating it to the Name table.

       

        I would like to audit the Person entity in such a way that the Person_AUD table can contain a Name_Id column. So I would like to audit the FK too but I don't want to audit the Name entity.

       

      If I audit the Person entity, including the relationship to Name, but I don't audit the name entity I get the following error:

      org.hibernate.MappingException: An audited relation from com.my.Person.name to a not audited entity com.my.Name! Such mapping is possible, but has to be explicitly defined using @Audited(targetAuditMode = NOT_AUDITED).

       

      This makes sense, envers audits entities not tables. But is there a way in which I can also audit the Name_Id column from the Person table in the Person_AUD revision table?

        • 1. Re: Can I audit only a foreign key and not the reffered entity?
          hernanbolido

          Hi!

           

          You can audit the relation from Person to Name whitout auditing Name entity marking that relation as @Audited(targetAuditMode=RelationTargetAuditMode.NOT_AUDITED).

          Note that:

          • The history instances of Person are going to reference the current instance of Name.
          • If the relation changes, the related Name changes, but there is not historic instance of Name.
          • If you delete a Name and it's referenced by an historic instance, it will fail.

           

          Hope it will help.

           

          Regards. Hernán.

          • 2. Re: Can I audit only a foreign key and not the reffered entity?
            florin13

            Is there any way in which I can audit the actual foreign keys that existed when the history entries were created? I mean, can I have any history instance of Person reference the instance of Name that existed at the moment of its creation?

             

            Having all the Person histories reference the current instance of name is of no use to me.

            • 3. Re: Can I audit only a foreign key and not the reffered entity?
              florin13

              This is how I would like the Person and Person_AUD tables to look like.

              Before:

                           Person: Id=3, Name_Id=234

                           Person_AUD: Id=3, REV=1, REVTYPE=0, Name_Id=234

              After (changing the name):

                           Person: Id=3, Name_Id=345

                           Person_AUD:Id=3, REV=1, REVTYPE=0, Name_Id=234

                                                 Id=3, REV=2, REVTYPE=1, Name_Id=345

              • 4. Re: Can I audit only a foreign key and not the reffered entity?
                adamw

                That's what you will have if you use @Audited(targetAuditMode=RelationTargetAuditMode.NOT_AUDITED).

                 

                This will work as expected as long as the Name entity is immutable (that is, a name never changes, but a reference from a Person to a Name can change).

                 

                Adam