5 Replies Latest reply on Jun 21, 2011 2:25 AM by adamw

    Auditing a Many-To-Many Relation

    n.mirzadeh

      I've tried to configure a relation to be audited without any success. I appreciate any help.

       

      I'm using Hibernate 3.5.6, and my mapping is in XML file. Here is how I've defined the auditing.

       

      @Audited

      public class CardType{

      ...

       

            @AuditJoinTable(name="CAMS_X_TYPE_CARD_ADDON_FILE",

              inverseJoinColumns={@JoinColumn(name="ID_ADDON_FILE"),

              @JoinColumn(referencedColumnName="ID_TYPE_CARD")})

          private Set<AddonFile> addonFiles = new HashSet<AddonFile>(0);

      ...

       

      ----------------------  Hibernate Mapping ------------------------------------

      <hibernate-mapping default-access="field">

          <class lazy="false" name="com.bellid.cams.model.CardType" table="CAMS_TYPE_CARD">

              <cache region="cams-configuration" usage="read-write"/>

              <id name="id" type="java.lang.Long">

                  <column name="ID" precision="10" scale="0"/>

                  <generator class="com.bellid.common.dataaccess.impl.identitygenerator.SequenceStyleGenerator">

                      <param name="sequence_name">TYPE_CARD_SEQ</param>

                      <param name="hibernate_sequence">TYPE_CARD_SEQ</param>

                      <param name="initial_value">10000</param>

                      <param name="increment_size">1</param>

                  </generator>

              </id>

              

              <set    name="addonFiles"

                      inverse="false"

                      table="CAMS_X_TYPE_CARD_ADDON_FILE"

                      lazy="false">

                  <key>

                      <column name="ID_TYPE_CARD" not-null="true" precision="10" scale="0"/>

                  </key>

                  <many-to-many entity-name="com.bellid.cams.model.AddonFile">

                      <column name="ID_ADDON_FILE" not-null="true" precision="10" scale="0"/>

                  </many-to-many>

              </set>

      ...

       

        • 1. Re: Auditing a Many-To-Many Relation
          adamw

          As I said in the PM, you didn't mention what the error actually is

           

          Adam

          • 2. Re: Auditing a Many-To-Many Relation
            n.mirzadeh

            The history of my audit-annotation:

            1)

            @NotAudited 

            private Set<AddonFile> addonFiles = new HashSet<AddonFile>(0);

             

            The relation is saved.

            -------------------------------------------------------

            2)

                @AuditJoinTable(name="CAMS_X_TYPE_CARD_ADDON_FILE",

                                         inverseJoinColumns={@JoinColumn(name="ID_ADDON_FILE"),

                                        @JoinColumn(referencedColumnName="ID_TYPE_CARD")})

                private Set<AddonFile> addonFiles = new HashSet<AddonFile>(0);

             

            Raised this error at the initialization of the listeners

            org.hibernate.MappingException: An audited relation from com.bellid.cams.model.CardType.addonFiles to a not audited entity com.bellid.cams.model.AddonFile! Such mapping is possible, but has to be explicitly defined using @Audited(targetAuditMode = NOT_AUDITED).

            ------------------------------------------------------------

            3)  The following will cause an error because of insertion of "NULL" into a required field.

             

                @Audited(targetAuditMode=RelationTargetAuditMode.NOT_AUDITED)

                @AuditJoinTable(name="CAMS_X_TYPE_CARD_ADDON_FILE",

                                inverseJoinColumns={@JoinColumn(name="ID_ADDON_FILE"),

                                                    @JoinColumn(referencedColumnName="ID_TYPE_CARD")})

                private Set<AddonFile> addonFiles = new HashSet<AddonFile>(0);

            • 3. Re: Auditing a Many-To-Many Relation
              adamw

              First of all, you should consider if the AddonFile should be audited or not. Generally, RelationTargetAuditMode.NOT_AUDITED should only be used when the other side is a dictionary-type entity.

               

              If you are sure you want to use it, I don't quite understand what is the error in the 3) case? Where is the null inserted?

               

              Adam

              • 4. Re: Auditing a Many-To-Many Relation
                n.mirzadeh

                Hi Adam,

                If you read the error message, it clearly says that AddonFile is not audited! In other words, it is treated as dictionary type entries as you would say. In general,  it should be possible to audit any side of a many-to-many relation! Case 2) didn't work. I put case 3) just to show that I've done what the error-message suggested.

                 

                So, my question is why I get that error in case 2)?, and what is the correct way to annotated the above relation.

                • 5. Re: Auditing a Many-To-Many Relation
                  adamw

                  As the error message says, in such a case (where only one side of a relation is not audited), you should use: @Audited(targetAuditMode=RelationTargetAuditMode.NOT_AUDITED).

                   

                  So I'm wondering why you get the error and what it exactly is. It is of course possible that Envers has some bug in that area, I just can't see it yet .

                   

                  Adam