我正在尝试打印此 MonthlyReport 中的所有日期,但出现此异常。
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
,您需要将 Entity 添加到您的查询对象中。
下面试试——
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/