java.lang.ClassCastException:[Ljava.lang.Object;与com.spring.model.Instruction

时间:2016-09-03 15:06:26

标签: java hibernate

public List<Instruction> listPAyment() {
    Session session = this.sessionFactory.getCurrentSession();
    List<Instruction> personsList = (List<Instruction>)session.createSQLQuery(
        "SELECT INSTRUCTIONKEY, BASECURRENCY,STATUSPROC, WHENMODIFIED FROM MyDB.INSTRUCTION"
    ).list();
    EXCEPTION HERE--->    for(Instruction p : personsList){
        System.out.println( "Payment::"+ p.toString()); 
    }
    return personsList;
}

我在上述问题上得到java.lang.ClassCastException: [Ljava.lang.Object; incompatible with com.spring.model.Instruction例外,我无法找出在下面的代码中投射有什么问题。 我已在模型类toString()

中正确添加了Instruction

请协助

[2016/09/05 15:05:01:991 GMT + 02:00] 00000040 ServletWrappe E com.ibm.ws.webcontainer.servlet.ServletWrapper服务SRVE0068E:其中一种服务方法抛出异常应用程序[SpringMVCHibernate_war]中的servlet [/WEB-INF/views/instruction.jsp]。创建异常:[java.lang.NumberFormatException:对于输入字符串:“instructionKey”     at java.lang.NumberFormatException.forInputString(NumberFormatException.java:59)     在java.lang.Integer.parseInt(Integer.java:460)     在java.lang.Integer.parseInt(Integer.java:510)     在javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)     在javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)     在javax.el.CompositeELResolver.getValue(CompositeELResolver.java:55)     在org.apache.el.parser.AstValue.getValue(AstValue.java:174)     at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:283)     at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:778)     at com.ibm._jsp._instruction._jspx_meth_c_forEach_0(_instruction.java:131)     在com.ibm._jsp._instruction._jspx_meth_c_if_0(_instruction.java:179)     在com.ibm._jsp._instruction._jspService(_instruction.java:95)     在com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:99)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:668)     在com.ibm.ws.cache.servlet.ServletWrapper.serviceProxied(ServletWrapper.java:307)

Jsp迭代对象:

      <c:forEach items="${listPersons}" var="instruction">
            <tr>
                <td>${instruction.instructionKey}</td>
                <td>${instruction.statusProc}</td>
                <td>${instruction.baseCurrency}</td> 
                <td>${instruction.whenModified}</td>
            </tr>
        </c:forEach>

其中model.addAttribute(“instruction”,new Instruction());         model.addAttribute(“listPersons”,this.personService.listPersons());

在控制器中定义。

2 个答案:

答案 0 :(得分:0)

modify query as select * FROM MyDB.INSTRUCTION

instead of using  List<Instruction> personsList  

use  List<Object[]> personsList

你没有正确地迭代结果点击下面的链接

点击here

点击here

答案 1 :(得分:0)

你可以从数据库中提取一个名为:whatever:的类,并为该类提供从数据库中提取的变量,并为其提供一个可以调用的toString方法?可能?