org.hibernate.MappingNotFoundException [英] org.hibernate.MappingNotFoundException

查看:105
本文介绍了org.hibernate.MappingNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的休眠程序。我遵循此教程中给出的步骤。



我得到的错误是

  org.hibernate.MappingNotFoundException:resource:org.manu.dtd。 UserDetails在org.hibernate.cfg.Configuration.addResource(Configuration.java:799)上找不到
$ or $ $ $ $ $在org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2344)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
位于org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
位于org.hibernate.cfg.Configuration。在org.manu.dtd.TestHibernate.main(TestHibernate.java:16)上执行配置(Configuration.java:2243)
在org.hibernate.cfg.Configuration.configure(Configuration.java:2158)


这是我的文件夹结构

< img src =https://i.stack.imgur.com/lFEQl.pngalt =ente在这里你可以找到一些有关注释的例子:

 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配置DTD 3.0 // EN
http://www.hibernate.org/dtd/hibernate-configuration -3.0.dtd>

< hibernate-configuration>

< session-factory>
<! - 数据库连接设置 - >
< 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>密码< / property>
<! - JDBC连接池(使用内置) - >
< property name =connection.pool_size> 1< / property>

<! - - SQL方言 - >
< property name =dialect> org.hibernate.dialect.Oracle10gDialect< / property>

<! - 禁用二级缓存 - >
< property name =cache.provider_class> org.hibernate.cache.NoCacheProvider< / property>

<! - 将所有执行的SQL回复到stdout - >
< property name =show_sql> true< / property>

<! - 在启动时删除并重新创建数据库模式 - >
< property name =hbm2ddl.auto>建立< / 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;
导入org.hibernate.cfg.Configuration;

public class TestHibernate {

public static void main(String [] args){$ b $ UserDetails user = new UserDetails();
user.setUserId(23);
user.setUserName(Renu);
System.out.println(设置值完整);
尝试{
SessionFactory sF = new Configuration()。configure(hibernate.cfg.xml)。buildSessionFactory();
会话会话= sF.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction()。commit();
}
catch(HibernateException he){
he.printStackTrace();



$ b $ / code>

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

解决方案

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


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

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

<?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.

解决方案

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. Here is small example from the offical tutorials

这篇关于org.hibernate.MappingNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆