通用方法
// getAll
@SuppressWarnings("unchecked")
public <T> List<T> getAll(Class<T> entityClass) throws DataAccessException {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(entityClass);
return criteria.list();
}
在控制器中获取列表
List<GenCurrencyModel> currencyList=pt.getAll(GenCurrencyModel.class);
测试
System.out.println("Type: "+currencyList.get(0).getClass());
System.out.println("Value: "+((GenCurrencyModel)currencyList.get(0)).getId());
结果
Type: class com.soft.erp.gen.model.GenCurrencyModel
Value: 1
更改通用方法[使用投影]
@SuppressWarnings("unchecked")
public <T> List<T> getAll(Class<T> entityClass, String[] nameList) throws DataAccessException {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(entityClass);
ProjectionList pl = Projections.projectionList();
for (int i=0; i<nameList.length; i++) {
pl.add(Projections.property(nameList[i].toString()));
}
criteria.setProjection(pl);
return criteria.list();
}
在控制器中获取列表
String []list={"id","isoCode"};
List<GenCurrencyModel> currencyList=pt.getAll(GenCurrencyModel.class,list);
测试
System.out.println("Type: "+currencyList.get(0).getClass());
System.out.println("Value: "+((GenCurrencyModel)currencyList.get(0)).getId());
结果[java.lang.ClassCastException]
nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.soft.erp.gen.model.GenCurrencyModel]
更新我!
答案 0 :(得分:0)
您正在设置投标以请求id
和isoCode
,因此结果是Object
数组的列表,其中id
位于索引0和isoCode
处指数1。
尝试将每个结果转换为Object[]
并检查数组的内容。