正如问题标题所说,我试图制作一个投影标准,只查询几个表属性.
所以我有一个Person Table /类,它有大约40个属性.我希望我的标准获得动态数量的属性,比如10,11或12(SQL术语select firstname, lastname from person),我这样做:
Transaction tx = session.beginTransaction();
Criteria crit = session.createCriteria(Person.class);
crit.setCacheable(true);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("id"));
Criterias c = null;
for (int i = 0; i < checked.size(); i++) {
Attribute attr = checked.elementAt(i);
switch (attr) {
case LASTNAME:
projList.add(Projections.property("lastName"));
c = enumMap.get(attr);
if (c.isChanged()) {
String tmp = (String) c.getAnswer();
tmp = tmp.replace('*', '%');
crit.add(Restrictions.like("lastName", tmp));
crit.addOrder(Order.asc("lastName"));
}
case ...THE REST .....
}
crit.setProjection(projList);
retList = crit.list();
tx.commit();
return retList;
Run Code Online (Sandbox Code Playgroud)
并且它返回 …