当前位置: 代码迷 >> Web前端 >> 10、java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to xxx.action.entity.Category
  详细解决方案

10、java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to xxx.action.entity.Category

热度:1032   发布时间:2012-11-04 10:42:41.0
十、java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to xxx.action.entity.Category
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to xxx.action.entity.Category
	at chentailei.action.test.SSH_CategoryDao.findByParentId(SSH_CategoryDao.java:61)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
	at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
	at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
	at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
	at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
	at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
	at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


原因:

界面输出标签如:${categorys.name},需要的是:List<Category>,而HIbernate回调函数返回的是Object[],两者予盾。

解决:

1.将输出类型List<Category>,变更为:List<Object[]>,变更页面输出${categorys[1]},详细对比数组元素的位置;

2.将回调函数返回的Object[],强转为:List<Object[]>(不能强转为 List<Category>,因为SQL语句是多表连接查询,执行后会报上述错误),然后再遍历,通过Category对象赋值(即将 Object[]的元素值赋予Category属性),再添加至List<Category>,转换代码如下:

//执行回调对象,获取结果集
List<Object[]> list =
 (List)this.getHibernateTemplate().execute(action);
//转换
List<Category> prolist = new ArrayList<Category>();
for(int i=0; i<list.size(); i++){
 Object[] obs=list.get(i);
 Category cate = new Category();
 cate.setId((Integer)obs[0]);
 cate.setTurn((Integer)obs[1]);
 cate.setName((String)obs[2]);
 cate.setEnName((String)obs[3]);
 cate.setDescription((String)obs[4]);
 cate.setParentId((Integer)obs[5]);
 cate.setPnum((BigInteger)obs[6]);
 prolist.add(cate);
}


  相关解决方案