0

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?

0

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

You are correct Hibernate SQLQUery function returns the data in different format even if i am retrieve the data from order table. I think i need to change the query. Any way thanks.
1

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());
         }
}

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.