具有hibernate.cfg.xml的MappingException

时间:2019-01-04 07:01:36

标签: java hibernate

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

1 个答案:

答案 0 :(得分:1)

Microsoft Windows [Version 10.0.17134.472] (c) 2018 Microsoft Corporation. Alle Rechte vorbehalten. C:\Users\Marco>cd \ C:\>dir Datenträger in Laufwerk C: ist Windows Volumeseriennummer: 98EE-C713 Verzeichnis von C:\ 15.07.2018 12:13 <DIR> inetpub 19.06.2018 16:34 <DIR> Intel 12.04.2018 00:38 <DIR> PerfLogs 05.10.2018 12:15 <DIR> Program Files 30.12.2018 04:27 <DIR> Program Files (x86) 18.07.2018 16:39 <DIR> SWSetup 18.07.2018 16:51 <DIR> temp 19.09.2018 10:44 <DIR> Users 03.01.2019 23:07 <DIR> Windows 0 Datei(en), 0 Bytes 9 Verzeichnis(se), 174.164.725.760 Bytes frei C:\> 放在Movie.hbm.xml下,并按如下所示更改配置文件中的路径,

src/main/resources

希望这会有所帮助。