我正在用struts,hibernate和jsp开发我的网站。我从操作页面调用了一个在DAO页面中的函数,如下所示:

private List<Order> salesDetails = new ArrayList();
salesDetails = doctorDao.getInstance().getDoctorSalesDetails(SessionObj.getId(),activityGraph);


在我的dao函数中,我编写了这样的代码

public List getDoctorSalesDetails(int id,int activityGraph){
    List<Order> doctorSalesDetails=new ArrayList();
    try{
       SessionFactory sessionFactory =
                (SessionFactory) ServletActionContext.getServletContext().getAttribute(HibernateListener.KEY_NAME);
        Session Hibernatesession = sessionFactory.openSession();
        Hibernatesession.beginTransaction();
        doctorSalesDetails =    Hibernatesession.createSQLQuery("SELECT total_amount,created_at FROM `order` WHERE created_at > DATE_SUB(curdate(),INTERVAL "+activityGraph+" DAY) AND doctor_id = "+id+" GROUP BY created_at").list();
        Hibernatesession.getTransaction().commit();
    }catch(Exception e){
        e.printStackTrace();
    }
    return doctorSalesDetails;
}


查询结果在这里成功工作。问题是当我从操作页面访问return变量时,如下所示:

    try{
       for( Order o:  salesDetails) {
           System.out.println("Total amount="+o.getCreatedAt());
       }
    }catch(Exception e){
        e.printStackTrace();
    }


它导致以下错误:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.myDrDirect.hbmobj.Order
    at com.myDrDirect.doctor.action.DoctorDashBoardActivity.getDashBoardActivityDetails(DoctorDashBoardActivity.java:36)


可能是什么问题?

最佳答案

private List<Order> salesDetails = new ArrayList();

salesDetails = doctorDao.getInstance().getDoctorSalesDetails(SessionObj.getId(),activityGraph);


在上面的语句中,salesDetails必须接受类型为“ Order”的元素。

doctorSalesDetails =    Hibernatesession.createSQLQuery("SELECT total_amount,created_at FROM `order` WHERE created_at > DATE_SUB(curdate(),INTERVAL "+activityGraph+" DAY) AND doctor_id = "+id+" GROUP BY created_at").list();


在上面的行中,doctorSalesDetails的类型为“ Object”,并且返回的是salesDetails。

因此,根据您的陈述

预期要持有订单的salesDetails持有对象。

结果,它抛出ClassCastException。

07-28 08:25