7 Replies Latest reply on Feb 8, 2010 11:19 AM by adamw

    Issue with Envers and composite foreign key

      Hello,

       

      I'm currently facing an issue with a composite foreign key on an audited entity not being populated in the relevant audit table and am hoping someone here can give me a dig out.

       

      The composite key is a three column EmbeddedId that is used in all entities within the application.  The audited entity is a User, marked with @Audited, and it contains a Client entity, also marked as @Audited.  The annotation for the User->Client relationship is as follows:

       

      @OneToOne (fetch = FetchType.LAZY,cascade = CascadeType.ALL)
      private Client client;

       

      During general Hibernate usage this works fine - the implicit mapping of the embeddedId is used by Hibernate - the User table is generated as follows:

       

      Key1

      Key2

      Key3

      DataField1

      DataField2

      .

      .

      Client_Key1

      Client_Key2

      Client_Key3

       

      ... and the foreign key to the client table is maintained as expected.  However when envers audits changes to the User entity within the User_AUD table, the foreign key fields Client_Key1, Client_Key2, Client_Key3 are always populated with their initial (Constructor) values, -1,-1,-1, rather than the relevant foreign key values that exist on the User entity.

       

      The Client entity is used by various other entities within the system so I was hoping to avoid using explicit annotations to map back to the User entity - the implicit inferred default <relatedEntityName>_<relatedEntityKey> is much preferred in this situation.

       

      Can anyone shed some light on this?  Thanks.

       

      Regards,

      Jim.

        • 1. Re: Issue with Envers and composite foreign key
          adamw

          Could you create a small testcase and atttach it to a JIRA issue?

           

          Also, I don't quite understand how "using explicit annotations to map back to the User entity" would help - how would the mappings look like then?

           

          Adam

          1 of 1 people found this helpful
          • 2. Re: Issue with Envers and composite foreign key

            Adam,

             

            Thanks for getting back to me.  It turns out this issue is to do with lazy-loading - the Client was being lazily loaded - I assume some proxy that envers queries just had the initial -1,-1,-1 value (equivalent to null for my purposes) - when I changed the load to eager, envers gets the foreign key as expected.  I'm not quite sure if this should be dismissed as a hibernate issue or flagged as an envers issue - I would have thought envers querying the one-to-one relationship from the User to the Cliet would have been enough for the proxy to load the relevant details but I don't have enough knowledge of the hibernate / envers internals to know how this happens or should happen.

             

            When I mentioned being more explicit with the mappings I am somewhat green regarding hibernate annotations - what I meant was explicitly listing what column in the relationship owner maps to what column in the relationship target.

             

            Is it worth raising a JIRA on this?  My app is using Tapestry - what do you normally use - a skeletal Spring oriented test case?

             

            Regards,

            Jim.

            • 3. Re: Issue with Envers and composite foreign key
              adamw

              Hello,

               

              that certainly looks like an Envers issue - the proxy should be loaded and the values properly stored.

               

              As for the testcase, I always use a simple console-based application (I never wrote a Spring app ). But anything that has the mappings will do.

               

              Adam

              • 4. Re: Issue with Envers and composite foreign key
                I've put together a test case which is unable to replicate the issue I am talking about - i.e. in the test case envers correctly stores the composite key of the related 1:1 entity, and can therefore load the Client related to the User object when the AuditReader is used.  Debugging the non-test case, I can see that Hibernate is passing the -1,-1,-1 value to Envers (when I step through the envers source, the referenced state[] for the composite key contains -1,-1,-1) - it looks like this is some session management / caching issue that is local to my project setup and is difficult to replicate in a test case where the test case is a pure console app without the supporting framework.  I am using c3p0 though not sure if this is relevant.  I will raise a JIRA if I can get a working test case and until then consider it a local issue.  Thanks for the feedback.
                • 5. Re: Issue with Envers and composite foreign key

                  I ended up getting a reproducible test case for this - executable jar and source code uploaded to https://jira.jboss.org/jira/browse/ENVERS-77 - thanks.

                   

                  Regards,

                  Jim.

                  • 6. Re: Issue with Envers and composite foreign key

                    Hi,

                     

                    Is there anyone working in the Envers code able to look at this issue - it's going to pop up for us quite often and it would be a shame to have to set all relevant relations to non-lazy - thanks.

                     

                    Regards,

                    Jim.

                    • 7. Re: Issue with Envers and composite foreign key
                      adamw

                      I'm working on Envers from time to time, when I do get some free time from my other activities, but unfortunately I can't tell you when I'll be able to work on your problem.

                       

                      Adam