错误代码:
1 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Float 2 at org.hibernate.type.descriptor.java.FloatTypeDescriptor.unwrap(FloatTypeDescriptor.java:19) 3 at org.hibernate.type.descriptor.sql.RealTypeDescriptor$1.doBind(RealTypeDescriptor.java:46) 4 at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:74) 5 at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280) 6 at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275) 7 at org.hibernate.param.PositionalParameterSpecification.bind(PositionalParameterSpecification.java:51) 8 at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:628) 9 at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2001) 10 at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1915) 11 at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1893) 12 at org.hibernate.loader.Loader.doQuery(Loader.java:938) 13 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341) 14 at org.hibernate.loader.Loader.doList(Loader.java:2692) 15 at org.hibernate.loader.Loader.doList(Loader.java:2675) 16 at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507) 17 at org.hibernate.loader.Loader.list(Loader.java:2502) 18 at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502) 19 at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:392) 20 at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) 21 at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1489) 22 at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1445) 23 at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414) 24 at pw.fengya.test.TestHibernate.testHQL(TestHibernate.java:84) 25 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 26 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 27 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 28 at java.lang.reflect.Method.invoke(Method.java:483) 29 at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:389) 30 at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115) 31 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:167) 32 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor$$Lambda$201/1771687225.execute(Unknown Source) 33 at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40) 34 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:163) 35 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:110) 36 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:57) 37 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:83) 38 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$133/1144648478.execute(Unknown Source) 39 at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66) 40 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77) 41 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92) 42 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$136/1681595665.accept(Unknown Source) 43 at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) 44 at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) 45 at java.util.Iterator.forEachRemaining(Iterator.java:116) 46 at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) 47 at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512) 48 at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502) 49 at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) 50 at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) 51 at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) 52 at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) 53 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92) 54 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$133/1144648478.execute(Unknown Source) 55 at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66) 56 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77) 57 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92) 58 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$136/1681595665.accept(Unknown Source) 59 at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) 60 at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) 61 at java.util.Iterator.forEachRemaining(Iterator.java:116) 62 at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) 63 at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512) 64 at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502) 65 at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) 66 at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) 67 at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) 68 at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) 69 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92) 70 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$133/1144648478.execute(Unknown Source) 71 at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66) 72 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77) 73 at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:51) 74 at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43) 75 at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170) 76 at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154) 77 at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90) 78 at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:86) 79 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 80 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) 81 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) 82 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) 83 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
根据首行的异常信息可以得出,这个错误是由int转float类型错误,我的查询HQL语句如下:
1 void testHQL() { 2 String hql = "FROM Employee e where e.salary > ? AND e.dept = ? AND e.email like ?"; 3 Query<Employee> query = session.createQuery(hql); 4 5 Deptartment dept = new Deptartment(); 6 dept.setId(5); 7 //动态绑定参数 8 query.setParameter(0, 6000) 9 .setParameter(1, dept) 10 .setParameter(2,"%a%"); 11 12 List<Employee> emps = query.list(); 13 System.out.println(emps.size()); 14 }
后来经过查找,发现salary是float类型,直接传入一个int整型参数,无法自动转换成float类型,可以先将int型参数转换为String 类型,再转换为float类型:
1 void testHQL() { 2 String hql = "FROM Employee e where e.salary > ? AND e.dept = ? AND e.email like ?"; 3 Query<Employee> query = session.createQuery(hql); 4 5 Deptartment dept = new Deptartment(); 6 dept.setId(5); 7 //动态绑定参数 8 query.setParameter(0, Float.valueOf(6000)) 9 .setParameter(1, dept) 10 .setParameter(2,"%a%"); 11 12 List<Employee> emps = query.list(); 13 System.out.println(emps.size()); 14 }