org.hibernate.boot.MappingNotFoundException:找不到映射(RESOURCE) [英] org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found
问题描述
以下是我的hibernate.config.xml
<?xml version ='1.0'encoding ='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-configuration-3.0 .dtd>
< hibernate-configuration>
< session-factory>
< property name =connection.driver_class>
com.mysql.jdbc.Driver
< / property>
< property name =connection.url>
jdbc:mysql:// localhost:3306 / abc
< / property>
< property name =connection.username> root< / property>
< property name =connection.password>< / property>
< property name =dialect>
org.hibernate.dialect.MySQLDialect
< / property>
< property name =show_sql> true< / property>
< mapping
resource =HibernateExample / src / HibernateExposed / Resource / Person.hbm.xml/>
< / session-factory>
< / hibernate-configuration>
运行代码时出现错误org.hibernate.boot.MappingNotFoundException:映射(RESOURCE)不是找到了。
我尝试用下面的代码替换映射
<映射
resource =Resource / Person.hbm.xml/>
,并试图将映射xml保留在与hibernate.config.xml相同的位置。
< mapping
resource =Person.hbm.xml/>
在上述任何情况下,代码都可以找到我的Person.hbm.xml。
我的文件夹结构如下所示:
我在Stackoverflow上查看了所有其他的这个错误的答案,但是没有一种方法可以解决这个问题。任何帮助,高度赞赏。另外,还有什么方法可以进一步调试到粒度级别吗?
解决方案 请将 hibernate。 cfg.xml
在 src
文件夹的根目录下。 使用
< mapping resource =HibernateExposed / Person.hbm.xml/>
Hibernate使用 ClassLoader.getResourceAsStream(resourcePath)加载所有这些文件
resourcePath
- 是文件的路径
ClassLoader
试图访问IDE中 bin
或 build
文件夹根目录中的文件,或者Web应用程序的 jar
的根目录,或 war / WEB-INF / classes /
的根目录。这些都是班级路径的根源。
bin
是Eclipse编译文件的文件夹。将 src
文件夹的根目录编译为 bin
文件夹的根目录。您可以检查它。
举例
configure(hibernate.cfg .xml)
- bin / hibernate.cfg.xml
configure(xxx / hibernate.cfg.xml)
- bin / xxx / hibernate.cfg.xml
< mapping resource =HibernateExposed / Person.hbm.xml/>
对应 bin / HibernateExposed / Person.hbm.xml
对于 ClassLoader $,路径应该没有前导
/
C $ C>。 Hibernate试图删除前面的 /
。
这样的路径也是有效的
<映射资源= / HibernateExposed / Person.hbm.xml/>
更新
如果你不想在根目录中拥有它,你可以指定 hibernate.cfg.xml
的路径
new Configuration()。configure(HibernateExposed / hibernate.cfg.xml)
if你使用
新配置().configure()
它应该在类路径的根目录中。
I have been trying my hand to implement hibernate using a small example.
Below is my hibernate.config.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/abc
</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<mapping
resource="HibernateExample/src/HibernateExposed/Resource/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
When running the code I am getting error org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found.
I tried replacing mapping as below
<mapping
resource="Resource/Person.hbm.xml"/>
and also tried to keep the mapping xml at same location as hibernate.config.xml.
<mapping
resource="Person.hbm.xml"/>
Under none of above mentioned cases, code can find my Person.hbm.xml.
My folder structure looks as below
I looked at all other answers for this error on Stackoverflow but none of the approaches resolved this issue. Any help is highly appreciated. Also, is there any approach to debug this further to granular level?
Please, put hibernate.cfg.xml
in the root of src
folder.
And use
<mapping resource="HibernateExposed/Person.hbm.xml"/>
Hibernate loads all those files using ClassLoader.getResourceAsStream(resourcePath)
resourcePath
— is the path to the file
ClassLoader
tries to get access to the files in the root of bin
or build
folder in the IDE, or root of jar
, or root of war/WEB-INF/classes/
for web-applications. Those all are the root of the class path.
bin
is a folder where Eclipse compiles your files. The root of src
folder is compiled to the root of bin
folder. You can check it.
For an example
configure("hibernate.cfg.xml")
— bin/hibernate.cfg.xml
configure("xxx/hibernate.cfg.xml")
— bin/xxx/hibernate.cfg.xml
<mapping resource="HibernateExposed/Person.hbm.xml"/>
corresponds bin/HibernateExposed/Person.hbm.xml
A path should be without the leading /
for a ClassLoader
. Hibernate tries to delete the leading /
.
A path like this is valid too
<mapping resource="/HibernateExposed/Person.hbm.xml"/>
Update
You can specify path to the hibernate.cfg.xml
, if you don't want to have it in the root
new Configuration().configure("HibernateExposed/hibernate.cfg.xml")
if you use
new Configuration().configure()
it should be in the root of the class path.
这篇关于org.hibernate.boot.MappingNotFoundException:找不到映射(RESOURCE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!