gpt4 book ai didi

java - hibernate.cfg.xml 的 MappingException

转载 作者:行者123 更新时间:2023-12-02 10:24:55 26 4
gpt4 key购买 nike

我有一个与 hibernate-core 4.2.3.Final 一起使用的 Maven 项目。我已遵循此处提供的建议:

Where to place hibernate config?

Hibernate Tutorial - Where to put Mapping File?

并且src/main/resources中的配置文件如下所示:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/justHibernate</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">12345</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping resource="com/mycompany/app/model/Movie.hbm.xml" />
</session-factory>
</hibernate-configuration>

当映射文件与 Movie 类一起位于包 com.mycompany.app.model 中时:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mycompany.app.model.Movie" table="Movies">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="title" column="title"/>
<property name="director" column="director"/>
<property name="synopsis" column="synopsis"/>
</class>
</hibernate-mapping>

异常

配置找不到映射文件。

INFO: HHH000221: Reading mappings from resource: com/mycompany/app/model/Movie.hbm.xml
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com/mycompany/app/model/Movie.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:738)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2167)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2139)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2119)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2072)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1966)
at com.mycompany.app.MovieManager.initSessionFactory(MovieManager.java:22)
at com.mycompany.app.MovieManager.<init>(MovieManager.java:17)
at com.mycompany.app.App.main(App.java:27)

最佳答案

Movie.hbm.xml 放置在 src/main/resources 下,并按如下方式更改配置文件中的路径,

<mapping resource="Movie.hbm.xml" />

希望这有帮助。

关于java - hibernate.cfg.xml 的 MappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54034390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com