4 Replies Latest reply on Mar 7, 2010 9:44 AM by adamw

    Getting a revision based on date

      Hi

       

      I was wondering if I could get some advice on how to achieve the following:

       

      I would like to return a version of an entity based on a given as of date.  I had a look at the AuditQuery and tried a few things but didn't get very far (using the revisionDate).  I would appreciate it very much if someone could give me a pointer on how to achieve this.

       

       

      Thanks

      Amin

        • 1. Re: Getting a revision based on date
          aminmc

          Hi

           

          After some playing around I think I may have got it but I'm not 100% sure.  It would be great if someone could just verify:

           

             AuditReader reader = AuditReaderFactory.get(session);

                          return (Work)reader.createQuery().forRevisionsOfEntity(Work.class, true, true)

                                  .add(AuditEntity.id().eq(work.getId()))

                                  .add(AuditEntity.revisionProperty("timestamp")

                                  .ge(asOfDate.getTime())).getSingleResult();

           

          Is this ok?

           

          Cheers

          Amin

          • 2. Re: Getting a revision based on date
            adamw

            "Almost" .

             

            You have to limit your search results to 1 and order by the revision date as you'll get possibly many revisions, but you are only interested by the first one. And you need to find the highest timestamp *lower* than the given one, so you'll need to replace ge with le.

             

            Or, alternatively, you can obtain a revision number for a given date using AuditReader and then query using this revision number.

             

            Adam

            • 3. Re: Getting a revision based on date
              aminmc

              Hi Adam

               

              Thanks for your reply.  I tried your alternate suggestion that that worked like a charm!  I tried the single query approach couldn't get it to work.  Which approach would you recommend?  I normally don't say this but I'm looking for the approach that offers good performance.

               

              I tried this and obviously I'm doing something wrong

               

              (Work) reader.createQuery().forRevisionsOfEntity(Work.class, true, true)

                                      .add(AuditEntity.id().eq(work.getId()))

                                      .addOrder(AuditEntity.revisionProperty("timestamp").asc())

                                      .add(AuditEntity.revisionProperty("timestamp").le(asOfDate.getTime()))

                                      .add(AuditEntity.revisionNumber().minimize()).getSingleResult();

               

               

               

              Thanks

              Amin

              • 4. Re: Getting a revision based on date
                adamw

                Hello,

                 

                I think you don't need AuditEntity.revisionNumber().minimize(), just set the number of returned rows to 1 (the method's name is setSelectMaxRows or setMaxRows).

                 

                Both solutions are comparable in performance, the alternate executes 2 queries, but that's really a very small overhead. Unless you are doing thousands of such lookups every moment this shouldn't matter .

                 

                Adam