Hibernate投影抛出类CastException Hibernate 3x+Spring MVC 3x 第一部分
通用方法 结果 第二部分 通用方法的变化[使用投影] 结果[java.lang.ClassCastException] 为什么这个类抛出异常,因为 criteria.setProjectionpl返回条件,然后返回相同列表的条件。 如何动态控制这一点?Hibernate投影抛出类CastException Hibernate 3x+Spring MVC 3x 第一部分,hibernate,criteria,projection,classcastexception,hibernate-criteria,Hibernate,Criteria,Projection,Classcastexception,Hibernate Criteria,通用方法 结果 第二部分 通用方法的变化[使用投影] 结果[java.lang.ClassCastException] 为什么这个类抛出异常,因为 criteria.setProjectionpl返回条件,然后返回相同列表的条件。 如何动态控制这一点? 更新我 您正在将投影设置为请求id和等代码,因此结果是id位于索引0,等代码位于索引1的对象数组列表 尝试将每个结果强制转换为对象[],并检查数组的内容 // getAll @SuppressWarnings("unchecked") publi
更新我 您正在将投影设置为请求id和等代码,因此结果是id位于索引0,等代码位于索引1的对象数组列表 尝试将每个结果强制转换为对象[],并检查数组的内容
// 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());
nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.soft.erp.gen.model.GenCurrencyModel]