【问题标题】:MappingException with hibernate.cfg.xml带有 hibernate.cfg.xml 的 MappingException
【发布时间】:2019-01-04 07:01:36
【问题描述】:

我有一个与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>

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

<?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)

【问题讨论】:

  • com/mycompany/app/model/Movie.hbm.xml not found 表示文件丢失或文件放错位置意味着路径可能错误
  • 你把Movie.hbm.xml放在哪里了?是在src 下吗?

标签: java hibernate


【解决方案1】:

Movie.hbm.xml 放在src/main/resources 下,并在配置文件中更改路径,如下所示,

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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2013-07-30
    • 1970-01-01
    • 2019-11-10
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 2015-07-10
    • 1970-01-01
    相关资源
    最近更新 更多