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

J. Katzwinkel
  • 1,923
  • 16
  • 22
user2807385
  • 51
  • 1
  • 2
  • http://docs.jboss.org/envers/api-new/org/hibernate/envers/query/projection/class-use/AuditProjection.html `AuditProperty.count()`? –  Feb 20 '14 at 21:24
  • 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(). – user2807385 Feb 21 '14 at 19:48

1 Answers1

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()
adamw
  • 8,038
  • 4
  • 28
  • 32
  • 1
    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. – user2807385 Feb 21 '14 at 19:49
  • If you add a count projection to an otherwise normal query, you should get what you need, no? – adamw Feb 23 '14 at 12:01
  • 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. – user2807385 Feb 24 '14 at 18:40