How to get row count of audit table using hibernate envers
Asked Answered
M

1

5

In audit table, there is not such a Criteria that we could use Criteria.setProjection(Projections.rowCount()) to get the row count of the query.

We could use AuditQuery to do similar things. But I couldn't find how to set projections in this case. There is also a setProjection method for AuditQuery, but it takes AuditProjection as parameter. Is there a similar thing that I could setProjection(rowCount)?

Thanks

Musset answered 20/2, 2014 at 19:53 Comment(2)
docs.jboss.org/envers/api-new/org/hibernate/envers/query/… AuditProperty.count()?Hestia
It is possible to do a count on one field, but that is not I want. I want a row count for the final result. The should return the same result as AuditQuery.getResultList().size().Musset
S
12

You can do a count on a given field, e.g.:

getAuditReader().createQuery()
    .forRevisionsOfEntity(SomeEntity.class, false, true)
    .addProjection(AuditEntity.id().count()).getSingleResult()

Or you can count the revision numbers:

getAuditReader().createQuery()
    .forRevisionsOfEntity(SomeEntity.class, false, true)
    .addProjection(AuditEntity.revisionNumber().count()).getSingleResult()
Serena answered 21/2, 2014 at 11:56 Comment(3)
Yes, we could get a count on a given field by using the AuditProperty.count(). But, still, this is not give me the row count the entire query result.Musset
If you add a count projection to an otherwise normal query, you should get what you need, no?Serena
Hi Adam, addProjection(AuditEntity.id().count("id")).getSingleResult() gave me the correct count. At first, I thought this only gave me the count for the original entity ids.Musset

© 2022 - 2024 — McMap. All rights reserved.