无法将Object强制转换为Pojo类

时间:2017-01-24 12:19:07

标签: java sql-server stored-procedures eclipselink

在我的struts2项目中,我使用EclipseLink使用 StoredProcedureQuery 调用SQL Server过程。我的问题是在获取结果列表并将其转换为相应的POJO类之后,它显示编译错误:< / p>

  

java.lang.ClassCastException:[Ljava.lang.Object;无法施展   com.boban.UserForm

我知道 getResultList()方法返回对象数组。我的问题是,有没有办法将resultList转换为POJO类?

我的程序代码

List<UserForm> userlist = new ArrayList<UserForm>();
        StoredProcedureQuery query = em.createStoredProcedureQuery("USP_GET_USERLIST");
        query.registerStoredProcedureParameter("P_CUSTOMER_ID", String.class, ParameterMode.IN);
        query.setParameter("P_CUSTOMER_ID", customerid);
        List<UserForm> result = query.getResultList();
        for (UserForm userForm : result) {
            userlist.add(userForm);
        }

编辑:

我的更新代码

List result = query.getResultList();
        for (Object userObj : result) {
            userlist.add((UserForm) userObj);
        }

和错误:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.boban.UserForm
at com.service.UserServiceImpl.retrieveUserList(UserServiceImpl.java:469)
at boban.admin.action.UserAction.retrieveUserList(UserAction.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:891)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
at ognl.ASTMethod.getValueBody(ASTMethod.java:90)
at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at ognl.SimpleNode.getValue(SimpleNode.java:258)
at ognl.Ognl.getValue(Ognl.java:494)
at ognl.Ognl.getValue(Ognl.java:458)
at com.opensymphony.xwork2.ognl.OgnlUtil$2.execute(OgnlUtil.java:309)
at com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecute(OgnlUtil.java:340)
at com.opensymphony.xwork2.ognl.OgnlUtil.getValue(OgnlUtil.java:307)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:423)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:287)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:250)
at org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)

0 个答案:

没有答案