Criteria criteria = detachedCriteria
.getExecutableCriteria(openSession());
CriteriaImpl impl = (CriteriaImpl) criteria;
//Remove order clauses where query count.
//Order clause may cause some problem when we query count in some
//database.
//such as hsqldb.
List orderEntrys = new ArrayList();
try{
Field field = CriteriaImpl.class.getDeclaredField("orderEntries");
//field is private, so we must set it accessible when we access it.
field.setAccessible(true);
//Get orders
orderEntrys = (List) field.get(impl);
//Remove orders
field.set(criteria,new ArrayList());
}catch(Exception ex){
ex.printStackTrace();
}
int totalCount = ((Integer) criteria.setProjection(
Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
//add stored order clauses.
try{
Field field = CriteriaImpl.class.getDeclaredField("orderEntries");
field.setAccessible(true);
List innerOrderEntries = (List) field.get(criteria);
innerOrderEntries.addAll(orderEntrys);
}catch(Exception ex){
ex.printStackTrace();
}
List<T> items = criteria.setFirstResult(startIndex)
.setMaxResults(pageSize).list();
回复 更多评论