5 Replies Latest reply on May 4, 2010 6:53 AM by adamw

    How to get all revisions of an entity if only associated entity has changed?

    birnbuazn

      Hi,

       

      I'm currently evaluating Envers for a large scale project. I was following roughly the quickstart example from the doc with the Person and Address entities and did the following

       

      • Created a person1 => revision 1
      • Created a person2 => revision 2
      • Changed the address of person1 => revision 3
      • Changed the address of person1 again => revision 4

       

      Where I'm stuck: How do I get a list of all the revisions where person1 has changed (i.e., [1, 3, 4])?

       

      I do not want to query the revisions of the address, which might be feasible and simple in this easy domain model. Our domain model will be rather big, with dozens of entities on the person (including transitive entities as well). You could query all of them independently by walking the object tree of the person manually, but it would be rather nice if Envers would already do that for me.

        • 1. Re: How to get all revisions of an entity if only associated entity has changed?
          hernanbolido

          Hi!

           

          I think you can use tha AuditReader object. This interface do that sort of things...

           

          It's something like:

           

          ...

          AuditReader auditReader =  AuditReaderFactory.get(entityManager); // or AuditReaderFactory.get(session);

           

          auditReader.findRevision(YourClass.class, pkForYourObject);

           

          ...

           

           

          You can have a look at AuditReader interface, there are other methods for history queries.

           

          I hope it will help you. Hernán

          • 2. Re: How to get all revisions of an entity if only associated entity has changed?
            birnbuazn

            Hi Hernán,

             

            Unfortunately no. reader.getRevisions(Class, id) only returns the revisions when the entity itself has changed. In my case however, not the person entity itself changed, but the associated address entity.

             

            So reader.getRevisions(Class, id) only returns [1], where I would need a method that returns [1,3,4]

            • 3. Re: How to get all revisions of an entity if only associated entity has changed?
              hernanbolido

              Oh! I see...

               

              I think that's no possible to do automatically.

              Think in another object with a reference to the same object (address in this case)... Envers would know all other objects pointing to this and generate a revision for each one... and there is no way to know all entities that points to another...

              In that case I would perform a change in my master entity (a fake change) an envers will add a revision.

               

              Maybe Adam knows other way.

               

              Hope it helps. Hernán.

              • 4. Re: How to get all revisions of an entity if only associated entity has changed?
                birnbuazn

                Hi Hernán,

                 

                Hmmm. I think that's not what I want. Performing a fake change on my master entity would slow down every writing operation unnecessarily.

                So if revision information of the detail entity is not stored in the audit log of the master entity in the database, you have to collect this information from the audit logs of the detail entities at runtime by walking all (audited) associations of the master entity class. Or am I missing something here?

                 

                Walking the associations should be possible in a generic way. I know it would be kind of slow, but on the other hand in 99% of all the use cases I'm only reading current data anyway. So I could live with the fact that building the revision list for the master entity would take some time in this one use case. Any thoughts on this?

                 

                Stefan

                • 5. Re: How to get all revisions of an entity if only associated entity has changed?
                  adamw

                  Hello,

                   

                  Hernan is right that currently that's the only solution possible. I am aware of that limitation for quite some time . The solution would be of course to enable relation-traversing in queries, which is currently not possible, due to the complexity of history reading. This will be possible once the end-revision column support will be implemented, which is planned for a long time now, but unfortunately I still didn't have time to do it...

                   

                  Adam