简体   繁体   English

java类强制转换异常错误

[英]java class cast Exception Error

I'm developing my website in struts, hibernate and jsp. 我正在用struts,hibernate和jsp开发我的网站。 I had called a function which is in my DAO page from my action page like this: 我从操作页面调用了一个在DAO页面中的函数,如下所示:

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

and in my dao function I write the code like this 在我的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;
}

The query result is successfully working here. 查询结果在这里成功工作。 The problem is when I access the the return variable from my action page like this: 问题是当我从操作页面访问return变量时,如下所示:

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

it causes following error : 它导致以下错误:

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

What might be the issue? 可能是什么问题?

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

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

In the above statement salesDetails must accept elements which is of type "Order" . 在上面的语句中,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();

In the above line doctorSalesDetails is of type "Object" and the same thing you are returning to salesDetails. 在上面的行中,doctorSalesDetails的类型为“ Object”,并且返回的是salesDetails。

So as per your statement 所以根据您的陈述

salesDetails which is expected to hold a Order is holding an Object. 预期要持有订单的salesDetails持有对象。

As a result , its throwing ClassCastException . 结果,它抛出ClassCastException。

The fact that you're declaring the List to hold Order is only a compile time issue. 您声明列表保留Order的事实仅是编译时的问题。 If the list that's passed in doesn't contain Order objects (or Hibernate doesn't return them properly), you can run into this problem. 如果传入的列表不包含Order对象(或Hibernate无法正确返回它们),则可能会遇到此问题。 You're probably going to have to do something like this: 您可能需要执行以下操作:

for( Object obj:  salesDetails) {
         if(obj instanceof Order){
            Order o = (Order)obj;
           System.out.println("Total amount="+o.getCreatedAt());
         }
}

You're using Hibernate's native SQL scalar queries by invoking list() which returns a list of Object arrays, its values you can then map into your own types. 您正在通过调用list()来返回对象数组的列表,从而使用Hibernate的本机SQL标量查询 ,然后可以将其值映射到自己的类型。

If you want Hiberate to automatically map the entities for you, try entity SQL queries like so (assuming your entities map nicely from the fetched columns): 如果您希望Hiberate为您自动映射实体,请尝试这样的实体SQL查询 (假设您的实体从获取的列中映射得很好):

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").addEntity(Order.class);

You are using a combustible mixture of generics and non-generics, which is prone to all sorts of errors. 您正在使用泛型和非泛型的易燃混合物,这容易产生各种错误。

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

should be ArrayList<Order> . 应该是ArrayList<Order>

You seem to think that the hibernate call will return a list of Order objects, but it has apparently returned something more like List<Object[]> . 您似乎认为休眠调用将返回Order对象的列表,但显然已返回了类似List<Object[]> Read the documentation for the Hibernate API and learn what it is returning. 阅读有关Hibernate API的文档,并了解其返回的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM