3 Replies Latest reply on Nov 10, 2008 5:58 PM by amin1977

    Getting all revisions for Entity

      Hi

      I have started on a app service method to show the history of a given entity. I have done the following (which I think looks inefficient and not very good)

      List<Number> pcRevisionNumbers = reader.getRevisions(contact.getClass(), contact.getId());
       for (Number number : pcRevisionNumbers) {
       Contact c = reader.find(contact.getClass(), contact.getId(), number);
       historyResult.add(c);
       }
      


      this has been probably asked before...but is there a better way of doing this?

      any help would be appreciated.

      Thanks

        • 1. Re: Getting all revisions for Entity
          plaky

          Try something like

          VersionsReader reader = .. ;
          
          List objects = reader.createQuery().forRevisionsOfEntity(cls, true).add(
           VersionsRestrictions.idEq(primaryKey))
          




          • 2. Re: Getting all revisions for Entity
            adamw

            Exactly, this will return you a list of 3-element arrays which contain the entity at a given revision, the revision entity instance, and the revision type.

            See here for further reference:
            http://www.jboss.org/envers/queries.html

            Adam

            • 3. Re: Getting all revisions for Entity

              Thanks guys. I am now doing the following:


              VersionsReader reader = VersionsReaderFactory.get(hibernateTemplate.getSessionFactory().getCurrentSession());
               Contact contact = contactDao.findById(contactId);
               VersionsOrder versionOrder = asc ? RevisionProperty.asc(): RevisionProperty.desc();
               @SuppressWarnings("unchecked")List<? extends Contact> result = reader.createQuery().forRevisionsOfEntity(contact.getClass(),true, true).add(VersionsRestrictions.idEq(contact.getId())).addOrder(versionOrder).getResultList();
              
              


              As I have set the first param to true I should only get back the entites.

              Cheers