我正在使用 Spring 3.1.0、Hibernate 4、JDK 7 到 Tomcat 7,并在 itr.next() 方法上获得 ClassCastException。aaData 对象确实包含数据。
List<CustomerList> aaData = customerlistDaoimpl.getCustomerList();
/*
* putting data in a JSON form that DataTables recognizes
*/
String data = "{\"sEcho\": 3, \"iTotalRecords\": " + count + ",\"iTotalDisplayRecords\": " + count + ",\"aaData\": [ ";
Iterator<CustomerList> itr = aaData.iterator();
while(itr.hasNext()){
CustomerList cl = (CustomerList) itr.next();
data += "[\"" + cl.getName() + "\",\"" + cl.getAddress() + "\",\"" + cl.getZipcode() + "\",\"" +
cl.getPhone() + "\",\"" + cl.getCity() + "\",\"" + cl.getCountry() + "\",\"" + cl.getNote() + "\" ] ";
count++;
}
data += "]}";
我的道
@SuppressWarnings("unchecked")
@Override
public List<CustomerList> getCustomerList() {
List<CustomerList> cuList = null;
Session session = null;
try{
session = sessionfactory.openSession();
session.beginTransaction();
cuList = session.createSQLQuery("select * from customer_list").list();
session.getTransaction().commit();
}catch (RuntimeException e){
System.out.println(e.getMessage());
}
finally
{
if(session != null){
session.close();
}
}
return cuList;
}
和追溯
严重:servlet [sptestjs] 在路径 [/SPTestJs] 的上下文中的 Servlet.service() 引发异常 [请求处理失败;嵌套异常是 java.lang.ClassCastException: [Ljava.lang.Object; 无法转换为 com.sptestjs.implementation.CustomerList] 的根本原因 java.lang.ClassCastException: [Ljava.lang.Object; 无法在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke 的 com.sptestjs.implementation.controller.HomeController.getCustomerList(HomeController.java:85) 的 com.sptestjs.implementation.CustomerList (未知来源)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)
我确实找到了电话
SQLQuery cuSQLQuery = session.createSQLQuery("select * from customer_list");
返回一个 SQLQuery 实例,它的列表是 Object 元素的 ArrayList 类型,其中
Query cuQuery = session.createQuery("from customer_list");
返回null
。