我的代码是
private List find(String queryString) {
final Query query = getSession().createQuery(queryString.intern());
List resultSet = query.list();
return resultSet;
}
@SuppressWarnings("rawtypes")
public List < CrExcessMaster > getExcessesWidget(String primaryCO) {
List cifList = find("select cem.id.cifNumber from CrEntitymaster cem where cem.primaryCo='" + primaryCO + "'");
List < CrExcessMaster > temp = null;
for (int i = 0; i < cifList.size(); i++) {
log.info("cifList in dao " + cifList);
if (i == 0) {
temp = find("from CrExcessMaster where cif='" + cifList.get(i) + "'");
} else temp.add((CrExcessMaster) find("from CrExcessMaster where cif='" + cifList.get(i) + "'"));
}
return temp;
}
其中crExcessMaster是一个表,我需要显示变量临时值... 但是,当我尝试调用以上返回值时,如下所示
public static FilteredUIExcessList formatCustomerExcessUiBean(List < CrExcessMaster > crExcessMasterList) {
if (crExcessMasterList != null) {
FilteredUIExcessList filteredUIExcessList = new FilteredUIExcessList();
< font color = red >
for (CrExcessMaster crExcessMaster: crExcessMasterList) < /font>{
ExcessUIBean excessUIBean = new ExcessUIBean();
excessUIBean.setCurrency(crExcessMaster.getCurrency());
}
在以红色突出显示的代码中,它正在抛出分类广播异常
java.lang.ClassCastException: java.util.ArrayList cannot be cast to cib.ecm.creditRisk.excessManagement.db.orm.CrExcessMaster
完整堆栈跟踪
java.lang.ClassCastException:java.util.ArrayList无法强制转换为cib.ecm.creditRisk.excessManagement.db.orm.CrExcessMaster at cib.ecm.creditRisk.excessManagement.web.uiFormatters.UIBeanFormatter.formatCustomerExcessUiBean(UIBeanFormatter.java:65) at cib.ecm.creditRisk.excessManagement.web.services.ExcessManagementUiService.getExcessesWidget(ExcessManagementUiService.java:52) at cib.ecm.creditRisk.excessManagement.web.action.ExcessListActionBean.getExcessesWidget(ExcessListActionBean.java:105) 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 net.sourceforge.stripes.controller.DispatcherHelper $ 6.intercept(DispatcherHelper.java:442) at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:158) at net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor.intercept(BeforeAfterMethodInterceptor.java:113) at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155) at net.sourceforge.stripes.controller.ExecutionContext.wrap(ExecutionContext.java:74) at net.sourceforge.stripes.controller.DispatcherHelper.invokeEventHandler(DispatcherHelper.java:440) at net.sourceforge.stripes.controller.DispatcherServlet.invokeEventHandler(DispatcherServlet.java:285) at net.sourceforge.stripes.controller.DispatcherServlet.doPost(DispatcherServlet.java:167) at net.sourceforge.stripes.controller.DispatcherServlet.doGet(DispatcherServlet.java:67) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:707) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:820) 在org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) 在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1221) at net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:246) 在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1212) at cib.ecm.creditRisk.excessManagement.web.filter.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:35) 在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1212) 在org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399) 在org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) 在org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) 在org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766) 在org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450) at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230) 在org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114) 在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) 在org.mortbay.jetty.Server.handle(Server.java:326) 在org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection $ RequestHandler.headerComplete(HttpConnection.java:928) 在org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549) 在org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) 在org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) 在org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410) 在org.mortbay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:582)
答案 0 :(得分:3)
问题在于这一行:
} else temp.add((CrExcessMaster) find("from CrExcessMaster where cif='" + cifList.get(i) + "'"));
有了这个,您要向List
添加temp
,这将导致ClassCastException
稍后。而不是temp.add()
,请致电temp.addAll()
。