4 Replies Latest reply on Oct 1, 2012 8:51 AM by rlu

    Problems loading ManyToMany Relations

    rlu

      I have to Entities A and B with many

       

      {code}

      @Entity

      @Audited

      public class A {

           ...

           @ManyToMany(fetch = FetchType.EAGER)

           private Set<B> b;

      }

       

      @Entity

      @Audited

      public class B {

      ...

      }

      {code}

       

      Changes in relationship are written in audit table (A_B_AUD), trying to read an old revision of A AuditReader method forEntitiesAtRevisions returns an empty set for b.

      While using following code resultset contains one element of B, but in database are 2 elements

       

      {code}

      public class AuditLogReader {

          

           public <T> AuditedEntity<T> getAuditVersion(Class<T> type, long id, int revision) {       

                  AuditReader reader = getAuditReader();

                 

      Object[] revisionData = (Object[]) reader.createQuery()

                          .forRevisionsOfEntity(type, false, false)

                          .add(AuditEntity.id().eq(id))

                          .addOrder(AuditEntity.revisionProperty("id").desc())

                          .setMaxResults(1)

                          .getSingleResult();

                  return new AuditedEntity<T>((T)revisionData[0], (RevisionEntity) revisionData[1]);

           }

      }

      {code}

       

      The application is ejb3 deployed in jboss 7.1.1.FINAL (containing envers module 4.0.1-FINAL)

      maybe i miss something.

        • 1. Re: Problems loading ManyToMany Relations
          adamw

          Could you provide a more complete test-case, where entity insertions/modifications are visible?

           

          Adam

          • 2. Re: Problems loading ManyToMany Relations
            rlu

            Hi Adam,

             

            sorry for long response time. All insertions/modifications on collection are made through

            a.getBs().add(bInstance);

             

            While creating a standalone testcase envers works as expected. All revisions are returned with correct member sets.

            Differences between real and test code:

             

            1. Real Code Entities has all annotations on field, test code on method level. (changes on this in both projects causes errors in hibernate, don't know why )
            2. Real Code is deployed in JBAS7.1.1 with Oracle DB, Test code works outside container with h2.
            3. Real code uses an custom AuditData entity which extends DefaultRevisionEntity

             

            Robin

            • 3. Re: Problems loading ManyToMany Relations
              adamw

              Well, hard to say without a test case

              Maybe a starting point would be checking the hibernate errors if you change the annotations ...

               

              Adam

              • 4. Re: Problems loading ManyToMany Relations
                rlu

                Hi,

                 

                we solved our problem. The cause were our initial test data, we have created with plain SQL. So when an relationship references an initial object (with no history) the result set remains empty.

                Solution is to copy initial data to audittable with revision 1.

                 

                Robin