为什么我得到MappingNotFound异常

时间:2013-03-25 22:03:10

标签: java database hibernate mapping

你好我从映射我的hibernate得到异常: 初始SessionFactory创建failed.org.hibernate.MappingNotFoundException:资源:找不到bbstats / domain / User.hbm.xml 线程“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;
    }
}

目录结构: enter image description here

2 个答案:

答案 0 :(得分: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 根据您的域对象,以便于阅读。

答案 1 :(得分:1)

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