6 Replies Latest reply on Nov 28, 2009 3:09 AM by adamw

    An audited relation to a non-audited entity

      Hello,

      I have the following situation: I want to track changes in some fields of particular table. Some of these fields reference other entities which will not be audited (relation ManyToOne). For these fields I just need to record that instead of one primary key value in the related table audited record now references another primary key value. Assuming that primary key of the record normally doesn't change why envers requires that referenced entity is also marked as audited? Is it possible to avoid it?

      Thank you.

        • 1. Re: An audited relation to a non-audited entity
          hernanbolido

          Hello!

          I think that the property targetAuditMode of @Audited could help you to solve this.
          You can use:

          @Audited(targetAuditMode=RelationTargetAuditMode.NOT_AUDITED)

          In this case, the javadoc says:

          @return Specifies if the entity that is the target of the relation should be audited or not. If not, then when reading a historic version an audited entity, the realtion will always point to the "current" entity.
          This is useful for dictionary-like entities, which don't change and don't need to be audited.



          See the javadoc for the Audited annotation.



          Hernan.

          • 2. Re: An audited relation to a non-audited entity

            Hello Hernan,

            Thank you, it seems to be exactly what I need. I currently use the latest GA version 1.2.1 with Hibernate 3.3. This feature was added obviously after this release and is available in trunk as a part of hibernate core (3.5). Do you know if it is possible to build stand-alone envers that includes these extensions and is compatible with hibernate 3.3?



            • 3. Re: An audited relation to a non-audited entity
              hernanbolido

              Hi!
              Sure, there is a branch named envers-hibernate-3.3 compatible with hibernate 3.3
              It contains all bug fixes and new features that were added to trunk first.

              @Adam, are you planning a hibernate-3.3 compatible release for envers?


              Thanks. Hernan.

              • 4. Re: An audited relation to a non-audited entity

                Thank you again, Hernan.
                It seems there is a bug in the class ToOneRelationMetadataGenerator
                Old logic remained in the code. It placed after new coding and exception is thrown anyway:

                if (configuration == null) {
                 configuration = mainGenerator.getNotAuditedEntitiesConfigurations().get(referencedEntityName);
                 if (configuration != null) {
                 RelationTargetAuditMode relationTargetAuditMode = propertyAuditingData.getRelationTargetAuditMode();
                 if (!RelationTargetAuditMode.NOT_AUDITED.equals(relationTargetAuditMode)) {
                 throw new MappingException("An audited relation from " + entityName + "."
                 + propertyAuditingData.getName() + " to a not audited entity " + referencedEntityName + "!"
                 + ". Such mapping is possible, but has to be strictly defined using RelationTargetAuditMode.NOT_AUDITED in @Audited.");
                 }
                 }
                 }
                 if (configuration == null){
                 throw new MappingException("An audited relation from " + entityName + "."
                 + propertyAuditingData.getName() + " to a not audited entity " + referencedEntityName + "!");
                 }



                • 5. Re: An audited relation to a non-audited entity

                  Sorry, my mistake, still had old jar linked in one place

                  • 6. Re: An audited relation to a non-audited entity
                    adamw

                    Hello,

                    yes, I'm certainly planning on releasing new versions of Hibernate 3.3 compatible Envers, in some near future.

                    Meanwhile, you can use the snapshot, I deploy it quite regularly (last time, yesterday):
                    http://snapshots.jboss.org/maven2/org/jboss/envers/jboss-envers/1.2.2-hibernate-3.3-SNAPSHOT/

                    As hernanbolido wrote, specifying the targetEntityMode is the way to solve your problem, there is however one bug still unresolved, concerning many-to-many relations:
                    http://opensource.atlassian.com/projects/hibernate/browse/HHH-4090

                    Adam