gpt4 book ai didi

java.lang.ClassCastException : [Ljava. lang.Object;不能转换为 com.sakhnin.classes.MonthlySummary

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:07 25 4
gpt4 key购买 nike

我正在尝试打印此 MonthlyReport 中的所有日期并遇到此异常。

SEVERE: Servlet.service() for servlet showMettingReports threw exception java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.sakhnin.classes.MonthlySummary at org.apache.jsp.jspFiles.showMettingReports_jsp._jspService(showMettingReports_jsp.java:150) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318) at com.sakhnin.implementations.Controller.doGet(Controller.java:140) at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566) at 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) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to 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>

最佳答案

为避免此类ClassCastException,您需要将实体添加到您的查询对象中。试试下面--

SQLQuery query =  session.createSQLQuery("SELECT * FROM MonthlySummary WHERE MentorID="+id );

query.addEntity(MonthlySummary.class);

reports = query.list();
--rest of code

关于java.lang.ClassCastException : [Ljava. lang.Object;不能转换为 com.sakhnin.classes.MonthlySummary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com