5 Replies Latest reply on Jun 8, 2011 2:35 PM by adamw

    How to get Revisions for entities obtained with AuditReader.forEntitiesAtRevision()?

    sebp

      I've queried for all entities of type 'entityClass' that have a relation to another entity with name 'relatedEntityName' and id 'relatedEntityId' at a given revision:

       

      AuditQuery q = AuditReaderFactory.get(em).createQuery()

               .forEntitiesAtRevision(entityClass, revisionNumber)

               .add(AuditEntity.relatedId(relatedEntityName).eq(relatedEntityId));

      List result = q.getResultList();

       

      How can I retrieve the RevisionEntity for each result?

        • 1. Re: How to get Revisions for entities obtained with AuditReader.forEntitiesAtRevision()?
          adamw

          Ah, but wait, if you have the revision number, there is only one revision entity for the whole transaction, and you can obtain it using auditReader.findRevision. What you can't do right now easily is get the revision types for each entity (ADD, MOD, DEL).


          Adam

          • 2. Re: How to get Revisions for entities obtained with AuditReader.forEntitiesAtRevision()?
            sebp

            Yes, I have the revision number, but the forEntitiesAtRevision query returns all entities that exist at this revision. And for all of these entities I want to have the revision information of their last change. I solved it this way:

             

                    AuditReader reader = AuditReaderFactory.get(em);

             

                    // query for revision number and revision type of each entity

                    AuditQuery q = reader

                            .createQuery()

                            .forEntitiesAtRevision(entityClass, revisionNumber)

                            .addProjection(AuditEntity.revisionNumber())

                            .addProjection(AuditEntity.revisionType())

                            .add(AuditEntity.relatedId(relatedEntityName).eq(

                                    relatedEntityId));

                    List<Object[]> revisionNumberAndType = q.getResultList();

             

                    // query for the entities

                    q = reader

                            .createQuery()

                            .forEntitiesAtRevision(entityClass, revisionNumber)

                            .add(AuditEntity.relatedId(relatedEntityName).eq(

                                    relatedEntityId));

             

                    //

                    List<A> entities = q.getResultList();

                    List<Object[]> result = new ArrayList<Object[]>(entities.size());

                    for (int i = 0, size = entities.size(); i < size; i++) {

                        Object[] o = new Object[3];

                        o[0] = entities.get(i);

                        o[1] = reader.findRevision(AuditRevisionEntity.class,

                                (Number) revisionNumberAndType.get(i)[0]);

                        o[2] = revisionNumberAndType.get(i)[1];

                        result.add(o);

                    }

             

            Each element in the result list is an Object[] with three elements, the entity, its revision entity and the revision type. Is their an easier way to get this?

            • 3. Re: How to get Revisions for entities obtained with AuditReader.forEntitiesAtRevision()?
              adamw

              But the revision entity is all the same for each of those entities. So you are after the revision type, yes?

               

              Adam

              • 4. Re: How to get Revisions for entities obtained with AuditReader.forEntitiesAtRevision()?
                sebp

                No, the revision entities are different because the entities have changed at different times.

                • 5. Re: How to get Revisions for entities obtained with AuditReader.forEntitiesAtRevision()?
                  adamw

                  Ah, now I finally understood what you are trying to do - sorry .

                   

                  I don't think there's an easier way to do that right now. Feel free to post a JIRA feature request, maybe with a proposition of a nice general API that shold be implemented?

                   

                  Adam