我正在运行以下查询。它显示错误消息。如何解决这个错误?
List<Route>routeList=null;
List<?> companyList = session.createSQLQuery ("select name " +
"from company "+
"where company_id= " + companyId).list();
if(companyList.size() <= 0){
//throw(new AppException(1018,ErrorMessages.getString("INVALID_USER_ID")));
}
routeList = new ArrayList<Route>(companyList.size());
Route vgDetails=null;
for (int i = 0; i < companyList.size(); i++) {
vgDetails = new Route();
Object[] row = (Object[])companyList.get(i);
vgDetails.setRouteName ((String)row[0]);
routeList.add(vgDetails);
}
session.getTransaction().commit();
return routeList;
}
错误是:
java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
at com.claystone.server.user.UserListServiceImpl.getParticipantsDestination(UserListServiceImpl.java:902)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:527)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:166)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.doPost(RemoteServiceServlet.java:86)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:205)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
答案 0 :(得分:5)
你可以写:
vgDetails = new Route();
vgDetails.setRouteName ((String)companyList.get(i));
companyList
已经是一个字符串列表,因为您只选择一列。
此外,正如Thilo的回答所示,您可以将结果直接投射到List<string>
,而不是使用List<?>
。
答案 1 :(得分:2)
Hibernate返回一个String数组(也就是String类型的List)
List<?> companyList = session.createSQLQuery ("select name " +
"from company "+
"where company_id= " + companyId).list();
因为您的查询从Company表返回一个名称字符串。
您需要做什么才能返回列表
List<String> companyList = (List<String>)session.createSQLQuery ("select name " +
"from company "+
"where company_id= " + companyId).list();
更改您的错误:
vgDetails = new Route();
Object[] row = (Object[])companyList.get(i);
vgDetails.setRouteName ((String)row[0]);
routeList.add(vgDetails);
对此:
vgDetails = new Route();
vgDetails.setRouteName (companyList.get(i));
routeList.add(vgDetails);
as companyList.get(i)
返回一个String。
答案 2 :(得分:1)
您的companyList包含字符串,而不是对象[]。
你应该写
List<String> companyList = (List<String>) session.createSQLQuery (
"select name from company where company_id=?", companyId).list();
for (String routeName: companyList) {
vgDetails = new Route();
vgDetails.setRouteName (routeName);
routeList.add(vgDetails);
}