我试图在此MonthlyReport中打印所有日期并获得此例外。
SEVERE:Servlet showMettingReports的Servlet.service()抛出 异常java.lang.ClassCastException:[Ljava.lang.Object;不可能是 转到com.sakhnin.classes.MonthlySummary at org.apache.jsp.jspFiles.showMettingReports_jsp._jspService(showMettingReports_jsp.java:150) 在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:725)at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 在 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) 在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:725)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在 org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721) 在 org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466) 在 org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391) 在 org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318) 在com.sakhnin.implementations.Controller.doGet(Controller.java:140) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:618)at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)at at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 在 org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081) 在 org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:658) 在 org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:222) 在 org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1566) 在 org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1523) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source) 在 org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(未知来源)
java.lang.ClassCastException:[Ljava.lang.Object;无法施展 com.sakhnin.classes.MonthlySummary
MonthlySummary.java
public class MonthlySummary {
@Id
private String Date;
private int Cost;
private String Place;
private String Start;
private String End;
private String Description;
private int MentorID;
public MonthlySummary() {
// TODO Auto-generated constructor stub
}
public MonthlySummary(String place, String date, String start, String end,
String description, int cost, int mentorID) {
super();
Place = place;
Date = date;
Start = start;
End = end;
Description = description;
Cost = cost;
MentorID = mentorID;
}
MonthlySummaryIMP.java
public List<MonthlySummary> getMentorReportsById(int id) {
List<MonthlySummary> reports=null;
try{
session = factory.openSession();
session.beginTransaction();
// Fetch all active coupons whose businesses' parent's category is as got in the argument
SQLQuery query = session.createSQLQuery("SELECT * FROM MonthlySummary WHERE MentorID="+id );
reports = query.list();
closeSession();
}
catch(HibernateException e){
System.err.println(e.getMessage());
}
catch(Exception e){
System.err.println(e.getMessage());
}
return reports;
}
jsp文件
<tbody>
<%
List<MonthlySummary> m = (List<MonthlySummary>)request.getSession().getAttribute("meetingsReports");
for(MonthlySummary report : m) {
%>
<td>Date<%=report.getDate() %></td>
<td>Date<%=report.getPlace()%></td>
<td>Date<%=report.getStart()%></td>
<td>Date<%=report.getEnd()%></td>
<td>Date<%=report.getDescription() %></td>
<td>Date<%=report.getCost()%></td>
<%
}
%>
</tbody>
答案 0 :(得分:1)
要避免此类ClassCastException
,您需要将Entity添加到查询对象中。
请尝试以下 -
SQLQuery query = session.createSQLQuery("SELECT * FROM MonthlySummary WHERE MentorID="+id );
query.addEntity(MonthlySummary.class);
reports = query.list();
--rest of code
答案 1 :(得分:0)
由于您的堆栈跟踪表明它正在尝试转换MonthlySummary
的List,但实际上它无法理解左手对象来完成此过程。
这种奇怪的原因是如果你没有在第一只手上设置HTTPSession
属性,它应该给你Null Pointer Exception。
您确定没有使用该密钥设置任何属性,我的假设是您在该密钥中设置了一些其他对象。
请使用Sighr提到的技巧,这应该为你带来魔力。