-
1. Re: Getting a revision based on date
aminmc Mar 3, 2010 1:55 PM (in response to amin1977)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 Mar 4, 2010 8:42 AM (in response to aminmc)"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 Mar 4, 2010 2:20 PM (in response to adamw)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 Mar 7, 2010 9:44 AM (in response to aminmc)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