Hibernate异常之命名查询节点未找到

异常信息:

 1 java.lang.IllegalArgumentException: No query defined for that name [salaryEmps]
 2     at org.hibernate.internal.AbstractSharedSessionContract.getNamedQuery(AbstractSharedSessionContract.java:578)
 3     at org.hibernate.internal.AbstractSharedSessionContract.getNamedQuery(AbstractSharedSessionContract.java:103)
 4     at pw.fengya.test.TestHibernate.testNamedQuery(TestHibernate.java:59)
 5     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 6     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 7     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 8     at java.lang.reflect.Method.invoke(Method.java:483)
 9     at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:389)
10     at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
11     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:167)
12     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor$$Lambda$201/5021240.execute(Unknown Source)
13     at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
14     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:163)
15     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:110)
16     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:57)
17     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:83)
18     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$133/1144648478.execute(Unknown Source)
19     at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
20     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
21     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92)
22     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$136/1681595665.accept(Unknown Source)
23     at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
24     at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
25     at java.util.Iterator.forEachRemaining(Iterator.java:116)
26     at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
27     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
28     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
29     at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
30     at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
31     at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
32     at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
33     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92)
34     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$133/1144648478.execute(Unknown Source)
35     at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
36     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
37     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92)
38     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$136/1681595665.accept(Unknown Source)
39     at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
40     at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
41     at java.util.Iterator.forEachRemaining(Iterator.java:116)
42     at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
43     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
44     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
45     at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
46     at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
47     at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
48     at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
49     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92)
50     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$133/1144648478.execute(Unknown Source)
51     at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
52     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
53     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:51)
54     at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
55     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
56     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
57     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
58     at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:86)
59     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
60     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
61     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
62     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
63     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
64     Suppressed: org.hibernate.TransactionException: Transaction was marked for rollback only; cannot commit
65         at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:228)
66         at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
67         at pw.fengya.test.TestHibernate.destory(TestHibernate.java:48)
68         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
69         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
70         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
71         at java.lang.reflect.Method.invoke(Method.java:483)
72         at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:389)
73         at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
74         at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.invokeMethodInExtensionContext(ClassTestDescriptor.java:289)
75         at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$synthesizeAfterEachMethodAdapter$12(ClassTestDescriptor.java:281)
76         at org.junit.jupiter.engine.descriptor.ClassTestDescriptor$$Lambda$156/644166178.invokeAfterEachMethod(Unknown Source)
77         at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$null$9(TestMethodTestDescriptor.java:208)
78         at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor$$Lambda$205/1991386317.execute(Unknown Source)
79         at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
80         at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$13(TestMethodTestDescriptor.java:226)
81         at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor$$Lambda$203/1393112209.accept(Unknown Source)
82         at java.util.ArrayList.forEach(ArrayList.java:1234)
83         at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:224)
84         at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAfterEachMethods(TestMethodTestDescriptor.java:207)
85         at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114)
86         ... 48 more

学习Hibernate的命名查询时突然抛出异常,最后检查是由于自己太粗心了,query节点应该放在class节点外,与之同级

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 
 5 <hibernate-mapping package="pw.fengya.bean">
 6 
 7     <class name="Employee" table="t_EMPLOYEE">
 8 
 9         <id name="id" type="java.lang.Integer">
10             <column name="ID" />
11             <generator class="native" />
12         </id>
13 
14         <property name="name" type="java.lang.String">
15             <column name="NAME" />
16         </property>
17 
18         <property name="salary" type="float">
19             <column name="SALARY" />
20         </property>
21 
22         <property name="email" type="java.lang.String">
23             <column name="EMAIL" />
24         </property>
25 
26         <many-to-one name="dept" class="Deptartment" fetch="select">
27             <column name="DEPT_ID" />
28         </many-to-one>
29     
30         <query name="salaryEmps"><![CDATA[FROM Employee e WHERE e.salary > :minSal AND e.salary < :maxSal]]></query>
31     </class>
32 
33 </hibernate-mapping>

将query标签拿出来,与class同级即可

posted @ 2018-03-13 09:22  清汤白面  阅读(189)  评论(0编辑  收藏  举报