簡體   English   中英

Hibernate:映射未找到異常

[英]Hibernate: Mapping not found exception

從官方文檔開始Hibernate。 我陷入了配置xml文件的最基本步驟。 雖然我已按照示例中的描述完成了所有操作,但無法找到The mapping file

這是例外:

Exception in thread "main" org.hibernate.MappingNotFoundException: resource: testing/ground/beans/User.hbm.xml not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:728)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2115)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2087)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2067)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2020)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1935)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1914)
    at testing.ground.test.HibernateTest.main(HibernateTest.java:14)

帶main()的代碼:

package testing.ground.test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

    public class HibernateTest {
        private SessionFactory sessionFactory;

        public static void main(String[] args) {
            HibernateTest ht = new HibernateTest();
            ht.sessionFactory = new Configuration().configure().buildSessionFactory();
            ht.getData();
        }

        public void getData(){
            Session session = this.sessionFactory.getCurrentSession();
            session.beginTransaction();
            List list = session.createQuery("from users").list();
            System.out.println(list);
            session.getTransaction().commit();
            session.close();
        }



    }

Hibernate配置文件:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/somedb?characterEncoding=UTF-8</property>
        <property name="connection.username">username</property>
        <property name="connection.password"/>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <mapping resource="testing/ground/beans/User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

我試圖保持文件夾結構與Hiberante示例中的相同:

包裝結構的屏幕

請指教。

將user.hbm.xml文件放在testing.ground.beans包下的資源文件夾中

看起來你正在使用Maven來構建你的項目。 Maven期望資源(即非Java文件)位於src/main/resources 不是undr src/main/java

也就是說,找到另一個教程如何使用注釋來映射實體。 XML映射已過時,並且比注釋更難使用。 作為額外的獎勵,您還將學習如何使用EclipseLink和所有其他JPA引擎,因為注釋是標准的,而XML映射則不是。

User.hbm.xml移動到src/main/resources正確包( testing.ground.beans )。 我想你的構建工具不會從src/main/java選擇資源

你也可以使用<mapping file="src/main/java/testing/ground/beans/User.hbm.xml" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM