I have a pagination method to return a page:
Code:
@SuppressWarnings("unchecked")
public Page<T> getPage(final int pageNumber, final int pageSize, final Criteria criteria) {
int totalSize = 0;
if (pageSize > 0) {
// Save the original Projection and ResultTransformer if any
CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
Projection originalProjection = criteriaImpl.getProjection();
ResultTransformer originalResultTransformer = criteriaImpl.getResultTransformer();
criteria.setProjection(Projections.rowCount());
// TODO I don't like this intValue on a Long
// Why is uniqueResult returning a Long and setFirstResult requiring
// an int ?
totalSize = ((Long) criteria.uniqueResult()).intValue();
int startIndex = Page.getStartIndex(pageNumber, pageSize, totalSize);
criteria.setFirstResult(startIndex);
criteria.setMaxResults(pageSize);
criteria.setProjection(originalProjection);
criteria.setResultTransformer(originalResultTransformer);
}
List<T> list = criteria.list();
return new Page<T>(pageNumber, pageSize, totalSize, list);
}
As you can see in the method body, there is a:
totalSize = ((Long) criteria.uniqueResult()).intValue();
casting a long to an int.
I had to do this because uniqueResult() returns a long, indeed the size can be really long.
But the method setFirstResult expects an int.
So I had to do the casting.
Why is setFirstResult expecting an int instead of a long ?
Thanks.
Stephane