4

I am performing select option using Hibernate, Query is executing but getting exception at query.list() method,

Here is my code,

String hql="select a.vehicleno, a.lat, a.lng, a.status, a.rdate, a.rtime from LatitudeBean a, VehicleRegisterBean b where a.vehicleno=b.vehicleno and b.clientid= :clientId and b.groupid in(select groupid from GroupDetails where groupname= :groupname and clientid= :gdclientId)"; // valid query
Query query =sessio.createQuery(hql);
List<LatitudeBean> groupList = (List<LatitudeBean>)query.list(); //Here I am getting exception
for(LatitudeBean arr : groupList){
        System.out.println(arr.getVehicleno());
    }

Exception is,

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.aurodisplay.its.beans.LatitudeBean
at com.abc.its.controller.LoginController.doPost(LoginController.java:83)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)

How to cast list returning from list() method . Anyone help me in this please.

1
  • 1
    Post your exception stacktrace as well. Commented Aug 27, 2014 at 10:38

1 Answer 1

8

The problem is that your query does not select entities, but just properties of entities.

Therefore the result will not be a list of entities, but a list of object arrays (which arrays will hold the selected properties).

Try this:

List<Object[]> groupList = (List<Object[]>) query.list();
for(Object[] arr : groupList) {
    System.out.println("vehicleno: " + arr[0]);
}

Or if you want to select entire entities, modify your query like this:

String hql = "select a from LatitudeBean a, VehicleRegisterBean b where ...";

And that way your original code will work.

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

5 Comments

oh k . . then how can I change it.
Here all selected properties are from LatitudeBean. So do I need to change in query or in logic.?
Added how you can process the result.
cant we change for LatitudeBean because what I am selecting are all the only fields in LatitudeBean.
Yes, you can. Modify your query to return the "whole" entities. Added to the answer.

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.