简体   繁体   中英

java class cast Exception Error

I'm developing my website in struts, hibernate and jsp. I had called a function which is in my DAO page from my action page like this:

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

and in my dao function I write the code like this

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:

    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" .

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.

So as per your statement

salesDetails which is expected to hold a Order is holding an Object.

As a result , its throwing ClassCastException .

The fact that you're declaring the List to hold Order is only a compile time issue. 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. 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.

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):

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> .

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[]> . Read the documentation for the Hibernate API and learn what it is returning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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