4 Replies Latest reply on May 4, 2009 3:33 AM by brochaxc

    Can you query for changed Entities only?

      Is there a way to write a query to get all entities that were modified in a particular revision? For example - give me all users that were updated as part of revision X?

      Thanks,
      Brocha

        • 1. Re: Can you query for changed Entities only?
          adamw

          Hello,

          well, there's:

          auditReader.createQuery().forEntitiesAtRevision(entityClass, revisionNumber)
          

          to which you can add various Envers-query restrictions. See the docs and javadocs for more info. Is that what you need?

          Adam

          • 2. Re: Can you query for changed Entities only?

            I think that query will return all the entities - even those that were not changed in that particular revision.

            Lets say i run an update that sets a particular flag for 10 users in my users table. And that is recorded as revision 25.
            What i need is a way to get only those users that were updated as part of revision 25.

            I believe

            auditReader.createQuery().forEntitiesAtRevision(entityClass, revisionNumber)
            


            will return *all* users that existed at that point - even ones that were not updated.

            Thanks,
            Brocha

            • 3. Re: Can you query for changed Entities only?
              adamw

              Hello,

              ah yes, you are right.
              You could creating a query (revisions-of-entity even) and adding a restriction on the revision number, as if it was a normal property (so: AuditEntity.property("REV").eq(25)). This should work, I think. (replace "REV" with however you've named your revision number property/column)

              Adam

              • 4. Re: Can you query for changed Entities only?

                Hi Adam,
                Thanks for the reply. Actually what you wrote didn't work exactly but it gave me an idea and this gave me the result I wanted:

                Assuming User is an audited entity:

                List<User> users= (List<User>) reader.createQuery()
                 .forRevisionsOfEntity(User.class, true, true)
                 .add(AuditEntity.revisionNumber().eq(25))
                 .getResultList();
                
                


                The query above returns only users that were changed as part of revision 25.

                Another way that also worked but seems a little uglier:


                List<User> users= (List<User>)reader.createQuery()
                 .forEntitiesAtRevision(User.class, 25)
                 .add(AuditEntity.revisionNumber().eq(25))
                 .getResultList();
                


                Thanks,
                Brocha