我正在使用Hibernate执行select选项,Query正在执行但在query.list()
方法处获得异常,
这是我的代码,
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());
}
例外是,
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)
如何从list()
方法转换列表。任何人都可以帮助我。
答案 0 :(得分:8)
问题在于您的查询不会选择实体,而只会选择实体的属性。
因此,结果将不是实体列表,而是对象数组列表(哪些数组将保存所选属性)。
试试这个:
List<Object[]> groupList = (List<Object[]>) query.list();
for(Object[] arr : groupList) {
System.out.println("vehicleno: " + arr[0]);
}
或者,如果您想选择整个实体,请按以下方式修改您的查询:
String hql = "select a from LatitudeBean a, VehicleRegisterBean b where ...";
这样你的原始代码就可以了。