Using Hibernate Envers (4.1.9.Final). Trying to get all the revisions (date, revision number) for which a entities have changed of a certain type and that match a certain criterion.
This is the code that I'm currently having:
AuditReader auditReader = AuditReaderFactory.get(entityManager);
AuditQuery query = auditReader.createQuery()
.forRevisionsOfEntity(InventoryItem.class, false, true)
.add(AuditEntity.property("section_uuid").eq(sectionUuid))
.addOrder(AuditEntity.revisionNumber().desc());
List<Object[]> revisions = query.getResultList();
- This returns one element for each changed
InventoryItem
. So, if twoInventoryItem
s were changed in a revision, I get two elements -- I do not want that. - This returns also the actual
InventoryItem
s, I think that's a bit heavy -- I do not want that.
How can I get a distinct collection of revisions (date, revision number)?
I think what you are looking for is the
addProjection
method onAuditQuery
. You can add a projection on the revision number and date.On basis of Adam's answer, here is the code that I implemented. I'll mark his answer as the accepted answer.
As a result,
query.getResultList()
will return a collection ofObject[]
, where eachObject[]
contains:Object[0]
: revision number asint
Object[1]
: revision date asjava.util.Date
, corresponding thecreated
revisionProperty