java.lang.IllegalArgumentException异常 返回值类型的问题
1 java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return using requested result type [com.tao.pojo.Student] 2 at org.hibernate.internal.AbstractSharedSessionContract.resultClassChecking(AbstractSharedSessionContract.java:740) 3 at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:687) 4 at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:103) 5 at com.tao.test.StudentTest.test001(StudentTest.java:47) 6 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 7 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 8 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 9 at java.lang.reflect.Method.invoke(Method.java:498) 10 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 11 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 12 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 13 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 14 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 15 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 16 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 17 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 18 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 19 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 20 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 21 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 22 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 23 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 24 at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 25 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 26 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 27 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 28 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 29 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 30 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 31 32 org.hibernate.TransactionException: Transaction was marked for rollback only; cannot commit 33 at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:228) 34 at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68) 35 at com.tao.test.StudentTest.testAfter(StudentTest.java:32) 36 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 37 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 38 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 39 at java.lang.reflect.Method.invoke(Method.java:498) 40 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 41 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 42 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 43 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33) 44 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 45 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 46 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 47 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 48 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 49 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 50 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 51 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 52 at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 53 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 54 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 55 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 56 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 57 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 58 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
报这个错是时把 Student 类型换成 Object[]
错误的:
@Test
public void test001() {
Query<Student> query = session.createQuery("from Student s inner join s.clazz", Student.class);
List<Student> list = query.list();
for (Student stu : list) {
System.out.println(stu+","+stu.getClass().getName());
}
}
正确写法:
@Test
public void test001() {
Query<Object[]> query = session.createQuery("from Student s inner join s.clazz", Object[].class);
List<Object[]> list = query.list();
for (Object[] oo : list) {
System.out.println(oo+","+oo.getClass().getName());
}
}
第一行末尾 result type 就说了是返回值类型的问题,这里换成Object[]就OK了。