I am using the fancy new projection to get a count of rows, and then I want to re-run the same criteria as a list. This is just a simple pager.
It generally goes like this :
Code:
Criteria crit = getSession().createCriteria(getPersistentClass());
.. add critiera objects
crit.setProjection(Projections.rowCount());
return (Integer) crit.uniqueResult();
This leaves the critiera in a permanant 'projection' state, which I need to get out of.
The base Critieria interface has no methods for getProjection/getResultTransformer, only to set them, but the CritieraImpl itself does.
So this is what I do to get around this, which is basically just cast, save old values, and then re-set:
Code:
Long countByCriteria(Criteria crit ) {
ResultTransformer oldRes = ((CriteriaImpl) crit).getResultTransformer();
Projection oldProj = ((CriteriaImpl) crit).getProjection();
crit.setProjection(Projections.rowCount());
Long out = ((Integer) crit.uniqueResult()).longValue();
crit.setProjection(oldProj);
crit.setResultTransformer(oldRes);
return out;
}
Is this an oversight that will be changed, or is there a reason for this? There is no way for me to poke at projectionCritieria ( which is just set to 'this') so I'm not sure I'm not leaving something in a bad state.
Thanks,
Andrew Backer