Wenn Sie eine Projektion verwenden, wird der Rückgabetyp Objekt oder Objekt [] anstelle des Kriteriumstyps. Sie müssen einen Transformator verwenden.
Hier ist ein einfaches ResultTransformer:
private class ProjectionTransformer implements ResultTransformer {
private String[] propertysList;
private Class<?> classObj;
/**
* @param propertysList
*/
public ProjectionTransformer(String[] propertysList) {
this.classObj = persistentClass;
this.propertysList = propertysList;
}
/**
* @param classObj
* @param 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) {// convert message into a runtimeException, don't need to catch
throw new RuntimeException(e);
}
return retVal;
}
}
Hier ist, wie Sie es verwenden:
ProjectionList pl = (...)
String[] projection = new String[]{"Id","Description","Bid.Amount"};
crit.setProjection(pl).setResultTransformer(new ProjectionTransformer(projection));
ich es für die Beziehungen nicht getestet haben (zB: Bid.Amount).
Ich benutze 1.2. Ich werde versuchen zu sehen, ob das funktioniert. Vielen Dank. – sker