How to resolve java.lang.ClassCastException: for query.list () in hibernation

I am executing a select option using Hibernate, Query executes but gets an exception in the method query.list()

,

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

      

An exception:

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 pass a list returned from a method list()

. Someone help me with this please.

+3


source to share


1 answer


The problem is that your query is not allocating entities, but just properties of the objects.

Therefore, the result will not be a list of objects, but a list of arrays of objects (the selected properties will be saved in the arrays).

Try the following:

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

      



Or, if you want to select whole objects, change your query like this:

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

      

And this way your original code will work.

+8


source







All Articles