在我使用 Hibernate 的 Spring MVC 项目中,通过使用 Criteria API,我正在应用 Group BY 和 Order BY 子句。查询在 DB 上成功执行,它也带来了结果,但它是一个对象数组——
这是 Criteria API 的代码
Criteria criteria = session.createCriteria(DashboardSubindicatorSubmission.class, "DashboardSubindicatorSubmission")
.setProjection(Projections.projectionList()
.add(Projections.sum("InputValue").as("InputValue"))
.add(Projections.groupProperty("fkAccademicYearId"))
.add(Projections.groupProperty("fkAssessmentPlanID"))
.add(Projections.groupProperty("fkSubindicatorID"))
.add(Projections.groupProperty("InputTitle")))
.addOrder(Order.asc("fkAccademicYearId"))
.addOrder(Order.asc("fkAssessmentPlanID"))
.addOrder(Order.asc("InputTitle"));
List<DashboardSubindicatorSubmission> dashboardSubindicatorSubmissionList = (List<DashboardSubindicatorSubmission>)criteria.list();
session.flush();
transaction.commit();
return dashboardSubindicatorSubmissionList;
我正在铸造criteria.list()
,List<DashboardSubindicatorSubmission>
但是当我尝试dashboardSubindicatorSubmissionList.get(i)
在控制器上执行时,它给了我异常java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to mkcl.accreditation.model.DashboardSubindicatorSubmission
。
我知道了,尽管我将它转换为List<DashboardSubindicatorSubmission>
仍然是它的列表,object[]
这就是为什么我不能这样做dashboardSubindicatorSubmissionList.get(i)
,因为它返回我的对象DashboardSubindicatorSubmission
。(如果我错了,请纠正我)
那么如何将我的结果转换为DashboardSubindicatorSubmission
类列表?setResultTransformer() 在这种情况下对我有帮助吗?