I am working on pagination and trying to fetch rowcount for every Rest api call sending firstResult and MaxResults with url, for naming conventions I have mentioned firstResult as pageNumber and MaxResults as pageSize and the required piece of code looks like this, when the pageNumber is 1 (FirstResult is 0) the resultCount fetch me the correct counts but when I pass greater than 1 (FirstResult will be > 0) the count is returning me null :
Criteria criteria = session().createCriteria(Manufacturer.class);
criteria.add(Restrictions.eq("hospital.id", hospitalId));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
// Implementing pagination
if(pageNumber != null && pageSize != null){
criteria.setFirstResult((pageNumber - 1) * pageSize);
criteria.setMaxResults(pageSize);
}
List<Manufacturer> manufacturers = criteria.list();
if(manufacturers == null || manufacturers.isEmpty()){
return null;
}
// Reusing the same criteria for count
criteria.setProjection(Projections.rowCount());
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
Long resultCount = (Long)criteria.uniqueResult();
P.S : If there is any scope of improvement in the code I am open to it, will be thankful