Find the maximum revision of each object that is less than or equal to a given revision using envers

It may be simple, but unable to find a way. I am trying to find the maximum revision of each object less than or equal to the specified revision number.

AuditQuery query = getAuditReader().createQuery().forRevisionsOfEntity( entityClass, false, false); query.add(AuditEntity.revisionNumber().le(revisionNumber)); query.addOrder(AuditEntity.revisionNumber().desc()); query.getResultList(); 

Above the code returns multiple versions of the same object in descending order. I would like to receive the last distinct revision of each entity that is less than or equal to the specified revision number.

As a workaround, I am filtering the resultSet as shown below. I hope this filtering can be done in AuditQuery itself.

  AuditQuery query = getAuditReader().createQuery().forRevisionsOfEntity( entityClass, false, false); query.add(AuditEntity.revisionNumber().le(revision)); query.addOrder(AuditEntity.revisionNumber().desc()); List<?> list = query.getResultList(); StringBuilder builder = new StringBuilder(); EntityClass entity = null; Set<Long> entitySet = new HashSet<>(); if (!list.isEmpty()) { for (int i = 0; i < list.size(); i++) { Object[] result = (Object[]) list.get(i); entity = (EntityClass) result[0]; AuditRevision auditRevision = (AuditRevision) result[1]; RevisionType operationType = (RevisionType) result[2]; if (!entitySet.contains(entity.getId())) { //consider most recent revision of entity entitySet.add(entity.getId()); builder.append(i + "-->"); builder.append("id:" + entity.getId()); builder.append(", auditRevision:" + auditRevision); builder.append(", operationType:" + operationType); } } } builder.append(", count:" + entitySet.size()); 

Decision:

We need to use the fix [ https://hibernate.atlassian.net/browse/HHH-7827] ie AuditEntity.revisionNumber (). Maximize(). ComputeAggregationInInstanceContext ().

  AuditQuery query = getAuditReader().createQuery().forRevisionsOfEntity( entityClass, false, false); query.add(AuditEntity.revisionNumber().le(revision)); query.add(AuditEntity.revisionNumber().maximize() .computeAggregationInInstanceContext()); query.addOrder(AuditEntity.revisionNumber().desc()); return query.getResultList(); 
+2
hibernate hibernate-envers
source share
2 answers

I think you are looking for the AuditEntitry.revisionNumber.max() constraint. This should maximize the revision number that satisfies the nested constraints.

See also the documentation.

+2
source share

I was looking for something similar, but I found a much better way to get what I wanted. It took some time to solve this problem today, but now it makes sense!

In my case, I wanted to get objects on a specific date in the past. In Envers, this means with a certain revision, since the revision is universal for all objects, and not for the entity.

So, the idea is to find the version number from the date, and then get all the objects in this revision. I think this seems like what you need.

 AuditReader reader = AuditReaderFactory.get(this.entityManager); Number latestRevAtDate = reader.getRevisionNumberForDate(convertedRunDate); reader.createQuery() .forEntitiesAtRevision(MyEntity.class, latestRevAtDate) .add(AuditEntity.property("someEntityProperty").le("someValue")) .getResultList() 

You are only individual objects instead of several revisions of each object, and you get all the entities that were in the system during this revision.

This post helped a bit: https://developer.jboss.org/message/625074#625074

0
source share

All Articles