Java org.hibernate.MappingNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23834131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:29:44  来源:igfitidea点击:

org.hibernate.MappingNotFoundException

javahibernate

提问by rainu

I am trying to do a simple hibernate program. I am following the steps given in this tutorial.

我正在尝试做一个简单的休眠程序。我正在按照本教程中给出的步骤操作。

The Error that I am getting is

我得到的错误是

   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)

Here is my folder structure

这是我的文件夹结构

enter image description here

在此处输入图片说明

My persitence class with annotations is

我的带有注释的持久性类是

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

}

My hibernate.cfg.xml file is

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

And finally my main program is

最后我的主程序是

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();
        }
    }

}

Can anyone please help me out to resolve this issue.

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

采纳答案by Joel Witteveen

Well it looks it can be fixed even easier. You should use <mapping class=..>instead of <mapping resource=..>as resources is for mapping other xml files describing entities and such. Hereis small example from the offical tutorials

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

回答by Surendheran

Provide fully qualified name for hibernate configuration mapping file,

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

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