休眠:找不到映射异常 [英] Hibernate: Mapping not found exception

查看:89
本文介绍了休眠:找不到映射异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从其官方文档开始休眠.我被困在配置xml文件的最基本步骤.尽管我已按照示例中的说明进行了所有操作,但找不到The mapping file.

Started Hibernate from its official docs. I am stuck at the most basic step of configuring the xml files. Although I have done everything as described in the examples but it fails to find the 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()的代码:

The code with 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配置文件:

The Hibernate config file:

<?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示例中的相同:

I have tried to keep the folder structure same as that in the Hiberante examples:

请咨询.

推荐答案

将user.hbm.xml文件放在testing.ground.beans包下的资源文件夹中

place user.hbm.xml file in resource folder under testing.ground.beans package

这篇关于休眠:找不到映射异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆