1

你好,我从映射我的休眠中得到异常: Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: bbstats/domain/User.hbm.xml not found 线程“main”中的异常 java.lang.ExceptionInInitializerError

这是我的 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>

这是我的 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="bbstats.domain.Users" table="USERS">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <property name="title" column="nick"/>
    </class>

</hibernate-mapping>

在这里Users.class:

package bbstats.domain;

/**
 * Created with IntelliJ IDEA.
 * User: Rafał
 * Date: 25.03.13
 * Time: 13:33
 * To change this template use File | Settings | File Templates.
 */
public class Users
{
    private Long id;
    private String title;

    public Users() {}

    public Users(Long id, String title) {
        this.id = id;
        this.title = title;
    }

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

和 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;
    }
}

目录结构: 在此处输入图像描述

4

2 回答 2

1

问题出在您的User.hbm.xml中

这里

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

删除类的完全限定名称,因为您已经在此<hibernate-mapping package="bbstats.domain">行中提供了类的包名称

使用以下代码更新您的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 以便于阅读。

于 2013-03-26T05:24:58.153 回答
1

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

于 2013-09-09T18:45:11.607 回答