欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate报错:org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found :

程序员文章站 2022-03-02 13:32:06
...

1.具体报错原因:

java.lang.ExceptionInInitializerError
	at com.zrl.action.Test2.fun1(Test2.java:14)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : com.zrl.entity1.Dept : origin(com.zrl.entity1.Dept)
	at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)
	at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)
	at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
	at com.zrl.util.HibernateUtil.<clinit>(HibernateUtil.java:25)
	... 24 more

2.可能原因:

在hibernate.cfg.xml文件中引入实例的时候出错:

引入实例有两种形式:

①配置为xml形式引入:

<!-- 引入资源配置文件 -->
<mapping resource="com/zrl/entity/Clazz.hbm.xml"/>
<mapping resource="com/zrl/entity/Student.hbm.xml"/>

此种方式参数引用为resource

②直接引入实体类,但每个实体类必须要有@Entity注解

<!-- 将带有entity注解的实体类进行注册 -->
<mapping class="com.zrl.entity1.Dept"/>
<mapping class="com.zrl.entity1.Employee"/>

此种方式参数引用为class

③当然也有可能是路径拼写出错