简体   繁体   English

具有hibernate.cfg.xml的MappingException

[英]MappingException with hibernate.cfg.xml

I have a maven project that I am using with hibernate-core 4.2.3.Final . 我有一个与hibernate-core 4.2.3.Final一起使用的Maven项目。 I have followed the advice provided here: 我已遵循此处提供的建议:

Where to place hibernate config? 休眠配置在哪里?

Hibernate Tutorial - Where to put Mapping File? Hibernate教程-将映射文件放在哪里?

and the config file in src/main/resources looks like this: 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>

while the mapping file is within the package com.mycompany.app.model alongside the Movie class: 而映射文件位于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>

Exception 例外

The configuration cannot find the mapping file. 配置找不到映射文件。

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)

Place Movie.hbm.xml under src/main/resources and change path in your config file as follows, Movie.hbm.xml放在src/main/resources ,并按如下所示在配置文件中更改路径,

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM