When you use projection, the return type becomes the object or object [] instead of the criteria type. You must use a transformer.
Here is a simple ResultTransformer:
private class ProjectionTransformer implements ResultTransformer { private String[] propertysList; private Class<?> classObj; public ProjectionTransformer(String[] propertysList) { this.classObj = persistentClass; this.propertysList = propertysList; } public ProjectionTransformer(Class<?> classObj, String[] propertysList) { this.classObj = classObj; this.propertysList = propertysList; } @SuppressWarnings("unchecked") public List transformList(List arg0) { return arg0; } public Object transformTuple(Object[] resultValues, String[] arg1) { Object retVal = null; try { retVal = Class.forName(classObj.getName()).newInstance(); int dot = -1; for (int i = 0; i < resultValues.length; i++) { if ((dot = propertysList[i].indexOf(".")) > 0) { propertysList[i] = propertysList[i].substring(0, dot); } PropertyUtils.setProperty(retVal, propertysList[i], resultValues[i]); } } catch (Exception e) {
Here's how you use it:
ProjectionList pl = (...) String[] projection = new String[]{"Id","Description","Bid.Amount"}; crit.setProjection(pl).setResultTransformer(new ProjectionTransformer(projection));
I have not tested it for a relationship (e.g. Bid.Amount).
source share