Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
731 views
in Technique[技术] by (71.8m points)

hibernate - Find max revision of each entity less than or equal to given revision with envers

This might be simple, but unable to find a way. I am trying to find max revision of each entity less than or equal to given revision number.

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

Above code returns multiple revisions of same entity in descending order. I would like to get latest distinct revision of each entity less than or equal to given revision number.

As a workaround, I am filtering resultSet as shown below. I am hoping that this filtering can be done at 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());

Solution:

We need to use fix of [https://hibernate.atlassian.net/browse/HHH-7827] i.e 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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

See also the documentation on that matter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...