I'm using Spring 3.1.0, Hibernate 4, JDK 7, to a Tomcat 7 and getting a ClassCastException on the itr.next() method. The aaData object does contain the data.
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 += "]}";
My Dao
@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;
}
And the trace back
SEVERE: Servlet.service() for servlet [sptestjs] in context with path [/SPTestJs] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.sptestjs.implementation.CustomerList] with root cause java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.sptestjs.implementation.CustomerList at com.sptestjs.implementation.controller.HomeController.getCustomerList(HomeController.java:85) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
I did find the call
SQLQuery cuSQLQuery = session.createSQLQuery("select * from customer_list");
returns a SQLQuery instance and its list is of type ArrayList of Object elements where
Query cuQuery = session.createQuery("from customer_list");
returns null
.