6 Replies Latest reply on Sep 20, 2011 9:58 AM by sebp

    Auditing the not-owning side of a many-to-one relation

    sebp

      Hi,

      I have a problem creating revisions of the not-owning side of a many-to-one relation.

       

      Example:

       

      I have a many-to-one relation from entity B to entity A. Entity B looks like this:

       

      @Audited

      @Entity

      public class B {

          @ManyToOne

          @JoinColumn(name = "fk_a", nullable = false)

          private A a;

          ...

      }

       

      Entity A is not aware of B, meaning that there is no @OneToMany annotation in entity A. Even if revision_on_collection_change is set to true there is no new revision of A create when A is added or removed from B.

       

      If I add a one-to-many relation from A to B:

       

      @Audited

      @Entity

      public class A {

          @OneToMany(mappedBy="a")

          List<B> b;

          ...

      }

       

      then it works. Each time an A is added to B a new revision from A and B is created. I wan to have exactly this behaviour but without modelling the one-to-many relationship on A. Is that possible?

        • 1. Re: Auditing the not-owning side of a many-to-one relation
          adamw

          Currently not unfortunately. The whole idea is that without the @OneToMany, if you just add or remove a B, the A object is not modified in any way.

           

          But maybe let's step back, why would you want a revision for A created?

           

          Adam

          1 of 1 people found this helpful
          • 2. Re: Auditing the not-owning side of a many-to-one relation
            sebp

            Hi Adam,

            thanks for your reply. In real life my entities A and B are in different schemas. A must not know B because B is added through some kind of 'plug-in' functionality. As soon as B is available through the Plug-in, the user must be able to see all revisions of A in a dialog, choose a revision of A and click on a button that opens all Bs that where connected to A at the choosen revision. So the user should be able to do a horizontal audit from A to B.

            • 3. Re: Auditing the not-owning side of a many-to-one relation
              adamw

              So when listing revisions of A, you also want to include the revisions where related B's changed, not only A's? As otherwise you can simple do auditReader.getRevisionNumbers(A.class, aID)?


              Adam

              • 4. Re: Auditing the not-owning side of a many-to-one relation
                sebp

                Exactly. The revisions of A where Bs have changed should be shown.

                • 5. Re: Auditing the not-owning side of a many-to-one relation
                  adamw

                  Is be only one entity or are there potentially B1, B2, ..., Bn? If there's one, you could do a query to get all revisions of B's where the related A's id is some given value.

                   

                  Adam

                  • 6. Re: Auditing the not-owning side of a many-to-one relation
                    sebp

                    But the part of the program that wants to display the audit of A does not know B. Therefore it also doesn't know that it must consider B and the versions of A related to B when it displays the audit of A.

                     

                    I've had a look in the envers 1.2.2 source code and found out that the AuditEventListener.generateBidirectionalCollectionChangeWorkUnits(...) method checks if the relation between A and B is bidirectional. Since it is not, it skips the versioning of A. If I set the bidirectional property of the relation to true in debug mode, then auditing of A works fine. I'm thinking about removing the relDesc.isBidirectional() check. If I do this, will I face any problems? It seems not.