【问题标题】:hibernate is giving me - "main" org.hibernate.MappingNotFoundException: resource: hibernate_hbm.xml.UserDetails.hbm.xml not foundhibernate 给了我 - “main” org.hibernate.MappingNotFoundException: resource: hibernate_hbm.xml.UserDetails.hbm.xml not found
【发布时间】:2017-12-18 12:54:40
【问题描述】:

我正在使用休眠模式。我创建了一个名为 UserDetails(POJO 类)的表,其中包含 id 和名称。然而,我发现执行该程序很困难,因为它给了我这个错误 -

Exception in thread "main" org.hibernate.MappingNotFoundException: resource: 
hibernate_hbm.xml.UserDetails.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:740)
    at 
org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2197)
    at 
org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2169)
   at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2149)
   at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2102)
   at org.hibernate.cfg.Configuration.configure(Configuration.java:2017)
   at hibernate_hbm.xml.A.main(A.java:19)

所有文件都在 - hibernate_hbm.xml 包 - 我的文件是:

[1] 用户详细信息-

package hibernate_hbm.xml;


public class UserDetails {


  private int id;
  private String name;

 //setter & getters        

 }

[2]包含 UserDetails 对象和 SessionFactory 的 A.java 文件 -

package hibernate_hbm.xml;

 import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;

 public class A {

   public static void main(String[] args) {

    UserDetails user1 = new UserDetails();
    user1.setId(101);
    user1.setName("Mark");

    UserDetails user2 = new UserDetails();
    user2.setId(102);
    user2.setName("Cynthiya");

    SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(user1);
    session.save(user2);
    session.getTransaction().commit();
    session.close();

    }

 }

[3]hibernate.cfg.xml-

 <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
<session-factory>
    <property 
   name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property>
  name="hibernate.connection.url">jdbc:mysql:
  //localhost:3306/testingcampus</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <property 
   name="hibernate.current_session_context_class">thread</property>
    <property 
   name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <mapping resource="hibernate_hbm.xml.UserDetails.hbm.xml" />
    </session-factory>
  </hibernate-configuration>

[4] UserDetails.hbm.xml 文件 -

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="hibernate_hbm.xml.UserDetails" table="UserInfo">
   <id name="id"></id>
  <property name="name"></property>
</class>
</hibernate-mapping>

【问题讨论】:

  • 如果你在包hibernate_hbm.xml中有资源UserDetails.hbm.xml,它的资源路径应该是hibernate_hbm/xml/UserDetails.hbm.xml而不是hibernate_hbm.xml.UserDetails.hbm.xml(在hibernate.cfg.xml中)
  • @Alex 我输入了 - 在 UserDetails.hbm.xml 文件和 在 hibernate.cfg.xml 文件中。它显示错误 - 引起:org.hibernate.MappingException:在查找属性时找不到类 hibernate_hbm.xml.UserDetails.hbm.xml:id 和引起:java.lang.ClassNotFoundException:hibernate_hbm.xml.UserDetails.hbm。 xml

标签: java xml hibernate


【解决方案1】:

由于该文件位于类路径上,请尝试在 hibernate.cfg.xml 中映射资源值,如下所示:

“类路径:UserDetails.hbm.xml”

<mapping resource="classpath:UserDetails.hbm.xml" />

请分享您遵循的文件夹结构,因为这将有助于确定要使用的确切路径

【讨论】:

  • --> 文件夹结构为:hibernate_hbm.xml>>src>>hibernate_hbm.xml>A.java>UserDetails.java>UserDetails.hbm.xml
  • 请使用markdown标签格式化您的文件夹结构
  • @Alex 从项目开始 >hibernate_hbm.xml> src > hibernate3 > A.java & UserDetails.java &UserDetails.hbm.xml > hibernate.cfg.xml
  • @java_jazz 您实际上可以编辑您的原始帖子以添加任何缺失/请求的信息;这是推荐的方式,而不是将其放在 cmets 中。另外,请参阅stackoverflow.com/editing-help 了解有用的格式化选项(基本上,在显示文件夹结构时,将其格式化为与代码块相同的格式,以提高可读性)
猜你喜欢
  • 1970-01-01
  • 2016-08-30
  • 2019-05-12
  • 1970-01-01
  • 2018-09-11
  • 2020-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多