Java 为什么我得到MappingNotFound异常

Java 为什么我得到MappingNotFound异常,java,database,hibernate,mapping,Java,Database,Hibernate,Mapping,您好,映射我的hibernate时出现异常: 初始SessionFactory创建失败。org.hibernate.MappingNotFoundException:资源:bbstats/domain/User.hbm.xml未找到 线程“main”java.lang.ExceptionInInitializeError中出现异常 这是我的hibernate.cfg.xml: <?xml version='1.0' encoding='utf-8'?> <!DOC

您好,映射我的hibernate时出现异常: 初始SessionFactory创建失败。org.hibernate.MappingNotFoundException:资源:bbstats/domain/User.hbm.xml未找到 线程“main”java.lang.ExceptionInInitializeError中出现异常

这是我的hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
       <!DOCTYPE hibernate-configuration PUBLIC
               "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
               "http://hibernate.sourceforge.net/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://sql10.lh.pl:3306/zamocno_bb</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

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

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

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

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

        <property name="hbm2ddl.auto">validate</property>

        <mapping resource="bbstats/domain/User.hbm.xml"/>


    </session-factory>
</hibernate-configuration>
和HibernateUtil.class:

package bbstats.util;

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

public class HibernateUtil
{

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration()
                    .configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
目录结构:

问题出在您的User.hbm.xml

这里


删除类的完全限定名,因为您已经在此
行中提供了类的包名

使用以下代码更新您的User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="bbstats.domain">

    <class name="Users" table="USERS">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <property name="title" column="nick"/>
    </class>

</hibernate-mapping>

注意:
您的pojo名称是Users.java我建议您根据您的域对象将User.hbm.xml重命名为Users.hbm.xml,以便于阅读。

确保在Eclipse中java构建路径必须反映*/.xml。 右键单击项目>属性>Java构建路径>源代码>添加*/.xml
(生成路径上的源文件夹,包括**.xml)

尝试将映射文件移动到src/main/resources下的正确文件夹结构。请添加建议作为注释:)
package bbstats.util;

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

public class HibernateUtil
{

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration()
                    .configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
<class name="bbstats.domain.Users" table="USERS">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <property name="title" column="nick"/>
    </class>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="bbstats.domain">

    <class name="Users" table="USERS">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <property name="title" column="nick"/>
    </class>

</hibernate-mapping>