我正在将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;
}
追踪
SEVERE:上下文中Servlet[sptestjs]的Servlet.service(),路径为[/SPTestJs]引发异常[请求处理失败;嵌套异常为java.lang.ClassCastException:[Ljava.lang.Object;不能强制转换为具有根本原因的com.sptestjs.eimplementation.CustomerList]java.lang.ClassCastException:[Ljava.lang.Object;无法强制转换为com.sptestjs.implementation.CustomerListcom.sptestjs.eimplementation.controller.HomeController.getCustomerList(HomeController.java:85)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于的sun.reflect.NativeMethodAccessorImpl.invoke(未知源)sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)
我确实找到了的电话
SQLQuery cuSQLQuery = session.createSQLQuery("select * from customer_list");
返回一个SQLQuery实例,其列表的类型为ArrayList of Object元素,其中
Query cuQuery = session.createQuery("from customer_list");
返回CCD_ 1。
从我的迭代器下一个方法中获取ClassCastException
这意味着您的aaData
实际上不是List<CustomerList>
。您在某个地方进行了类型擦除,但您错误地更改了它的类型。如果你仔细查看ClassCastException,它会告诉你组件的真正类型
java.lang.ClassCastException:[Ljava.lang.Object;不能强制转换为com.sptestjs.implementation.CustomerList
这表明该类型实际上应该是
List<Object[]> cuList = session.createSQLQuery("select * from customer_list").list();
使用泛型。aaData可能包含不正确的数据
class CustomerlistDaoimpl{
public List<CustomerList> getCustomerList(){
.....
}
}
List<CustomerList> aaData = customerlistDaoimpl.getCustomerList();
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 += "]}";
检查您导入的CustomerList
类的完全限定类名是否与填充列表时使用的类名相同。我很好奇,既然您使用的是泛型,那么next()
应该返回与Iterator构建目的相同的类型,为什么您无论如何都需要进行强制转换。换句话说,当调用itr.next()
时,Iterator将始终返回T的实例。
已解决:
多个错误,异常是误导性的,我用一个不再存在的sessionFactory id限定了我的Dao-impl,同时自动连接了曾经修复过的sessionFactory到下一个错误。修复了所有这些问题,项目开始运行。