0

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: パス [/SPTestJs] のコンテキストでサーブレット [sptestjs] の Servlet.service() が例外をスローしました [要求処理に失敗しました。ネストされた例外は java.lang.ClassCastException: [Ljava.lang.Object; 根本原因 java.lang.ClassCastException: [Ljava.lang.Object; com.sptestjs.implementation.CustomerList] にキャストできません。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

4

4 に答える 4

2

イテレータの next メソッドから 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(); 
于 2013-08-02T05:59:32.613 に答える
0

CustomerListインポートしたクラスの完全修飾クラス名が、リストの入力中に使用したものと同じかどうかを確認してください。ジェネリックを使用しているのに、なぜキャストする必要があったのか不思議です-next()イテレータが構築されたものと同じ型を返す必要があります。つまり、Iteratoritr.next()が呼び出されると、常に T のインスタンスが返されます。

于 2013-08-03T18:13:02.417 に答える
0

ジェネリックを使用します。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 += "]}";
于 2013-08-02T05:59:27.973 に答える
0

Solved:

Multiple errors, the Exception was misleading, I had Qualified my Dao impl with a sessionFactory id that no longer existed, while auto wiring the sessionFactory that did once fixed on to the next bug. fixed them all and the project is up and running.

于 2013-08-06T00:43:45.530 に答える