Ten, java.lang.ClassCastException: [Ljava.lang.Object; cannot is cast to Xxx.action.entity.Category

Source: Internet
Author: User
Java.lang.ClassCastException: [Ljava.lang.Object; cannot being 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)

Reason:

Interface output label such as: ${categorys.name}, Need is: List<category>, and Hibernate callback function return is object[], both to shield.

Solve:

1. List<category> the output type, change to: list<object[]>, change page output ${CATEGORYS[1]}, compare the position of the array elements in detail;

2. Return the callback function object[], strong to:list<object[]> (not strong to LIST<CATEGORY> Because the SQL statement is a multiple-table join query, the execution will report the above error), and then Traverse, Add to List<category> by assigning the element value of the Category object (which will be object[] to the Category property), and converting the code as follows:

Executes the callback object, gets the result set
list<object[]> list =
 (list) this.gethibernatetemplate (). Execute (action);
Convert
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);
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.