3 Replies Latest reply on Nov 12, 2008 3:07 AM by Adam Warski

    Selecting Versions greater than the original

    Scott Robidoux Newbie

      Hi Adam,

      Thanks for all your help thus far. I have a small problem that I have been racking my brain on all day, and I think it is one of those where I have thought about it all day and confused myself and the answer is right in front of me.

      I want to select all the revisions that are not the first revision for the entity (i.e. only the changes that have been made after it was persisted).

      So far, I have tried a number of things with VersionsRestrictions, but I am having a tough time figuring out how to get the first revision as an Integer so the method is happy:

      VersionsQuery query = reader.createQuery()
      .forRevisionsOfEntity(EventType.class, false, true)
      .add(VersionsRestrictions.idEq(et2.getId()))
      .add(VersionsRestrictions.gt(RevisionProperty.revisionNumber(), RevisionProperty.min()))
      .addOrder(RevisionProperty.asc());
      


      Can you help out please?

      Thanks for all your help and quick responses.

      Scott

        • 1. Re: Selecting Versions greater than the original
          Sebastian Komander Newbie

          Hi,

          have you tried to use the RevisionTypeProperty-Projection?

          Look at this: :)

           Map<String, String> configurationOverrides = new HashMap<String, String>();
           EntityManagerFactory emf = Persistence.createEntityManagerFactory("ConsolePU", configurationOverrides);
           entityManager = emf.createEntityManager();
           versionsReader = VersionsReaderFactory.get(entityManager);
          
           Address addy = new Address();
           addy.setFlatNumber(1);
           addy.setHouseNumber(1);
           addy.setStreetName("initial");
          
          
           entityManager.getTransaction().begin();
           entityManager.persist(addy);
           entityManager.getTransaction().commit();
          
           for (int j = 0; j < 5; j++)
           {
           addy.setStreetName("change " + j);
           entityManager.getTransaction().begin();
           entityManager.merge(addy);
           entityManager.getTransaction().commit();
           }
          
           int id = addy.getId();
          
           System.out.println("reading all versions:");
           List<Address> resultsAll = versionsReader.createQuery().forRevisionsOfEntity(Address.class, true).add(
           VersionsRestrictions.idEq(id)).getResultList();
           for (Address a : resultsAll)
           {
           System.out.println(a.getStreetName());
           }
          
           System.out.println("reading only modificiations");
           List<Address> resultsMod = versionsReader.createQuery().forRevisionsOfEntity(Address.class, true).add(
           VersionsRestrictions.idEq(id)).add(VersionsRestrictions.eq("_revision_type", RevisionType.MOD))
           .getResultList();
           for (Address a : resultsMod)
           {
           System.out.println(a.getStreetName());
           }
          
           System.out.println("another way to read modificiations");
           int initialRevision = (Integer) versionsReader.createQuery().forRevisionsOfEntity(Address.class, false).add(VersionsRestrictions.idEq(id)).setProjection(RevisionProperty.min()).getSingleResult();
           List<Address> resultsAlternate = versionsReader.createQuery().forRevisionsOfEntity(Address.class, true).add(VersionsRestrictions.idEq(id)).add(RevisionProperty.gt(initialRevision)).getResultList();
           for (Address a : resultsAlternate)
           {
           System.out.println(a.getStreetName());
           }
           emf.close();
          


          And the output:


          reading all versions:
          initial
          change 0
          change 1
          change 2
          change 3
          change 4
          reading only modificiations
          change 0
          change 1
          change 2
          change 3
          change 4
          another way to read modificiations
          change 0
          change 1
          change 2
          change 3
          change 4


          • 2. Re: Selecting Versions greater than the original
            Sebastian Komander Newbie

            Ooops, I forget:

            With the first posibility "reading all modifications" there are really _only_ modifications, no deletes would be reported. The alternate way would show you all changes except the initial create.

            • 3. Re: Selecting Versions greater than the original
              Adam Warski Master

              Hello,

              I guess plaky gave you the full answer.

              The one point in which this may be made better, is to be able to use RevisionTypeProperty as a restriction. I'll add it in future versions.

              --
              Adam