org.hibernate.MappingNotFoundException

时间:2014-05-23 16:22:18

标签: java hibernate

我正在尝试做一个简单的hibernate程序。我正在遵循此tutorial中给出的步骤。

我得到的错误是

   org.hibernate.MappingNotFoundException: resource: org.manu.dtd.UserDetails not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:799)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2344)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
    at org.manu.dtd.TestHibernate.main(TestHibernate.java:16)

这是我的文件夹结构

enter image description here

带有注释的我的persitence类是

package org.manu.dtd;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserDetails {

    @Id
    private int userId;

    private String userName;
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }

}

我的hibernate.cfg.xml文件是

<?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">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
        <property name="connection.username">user</property>
        <property name="connection.password">password</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</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">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="org.manu.dtd.UserDetails"/>

    </session-factory>

</hibernate-configuration>

最后我的主要节目是

package org.manu.dtd;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class TestHibernate {

    public static void main(String[] args) {
        UserDetails user = new UserDetails(); 
        user.setUserId(23);
        user.setUserName("Renu");
        System.out.println("setting values complete");
        try {
        SessionFactory sF = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        Session session = sF.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
        }
        catch (HibernateException he) {
            he.printStackTrace();
        }
    }

}

任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:14)

看起来它可以更容易修复。您应该使用<mapping class=..>而不是<mapping resource=..>作为资源来映射描述实体等的其他xml文件。 Here是官方教程中的一个小例子

答案 1 :(得分:0)

为hibernate配置映射文件提供完全限定名称,

SessionFactory sF = new Configuration().
            configure("/org/manu/dtd/hibernate.cfg.xml").buildSessionFactory();