let developer can reuse DetachedCriteria perform a pre-counting before the actual query.
most of databases cannot execute aggregate function with order clause,so the order entries must remove when counting.
CriteriaImpl impl = detachedCriteria.getCriteriaImpl();
// remove order
List<OrderEntry> orderEntries = new ArrayList<OrderEntry>();
Iterator<OrderEntry> it = impl.iterateOrderings();
while (it.hasNext()) {
orderEntries.add(it.next());
it.remove();
}
// pre-counting
long totalRecord = countByCriteria(detachedCriteria);
// restore order
for (OrderEntry oe : orderEntries)
impl.addOrder(oe.getOrder());
// actual query
List results = findListByCriteria(detachedCriteria);
|