首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >hibernate给了我-“主”资源: org.hibernate.MappingNotFoundException: hibernate_hbm.xml.UserDetails.hbm.xml not found

hibernate给了我-“主”资源: org.hibernate.MappingNotFoundException: hibernate_hbm.xml.UserDetails.hbm.xml not found
EN

Stack Overflow用户
提问于 2017-12-18 20:54:40
回答 1查看 330关注 0票数 0

我正在使用hibernate。我创建了一个名为UserDetails (POJO类)的表,带有id和名称。然而,我发现很难执行这个程序,因为它给了我这个错误-

代码语言:javascript
复制
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包中- -my文件是:

1用户详细信息-

代码语言:javascript
复制
package hibernate_hbm.xml;


public class UserDetails {


  private int id;
  private String name;

 //setter & getters        

 }

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

代码语言:javascript
复制
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();

    }

 }

3hibernate.cfg.xml-

代码语言:javascript
复制
 <?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文件-

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2017-12-18 21:03:55

由于该文件位于类路径中,因此尝试使用hibernate.cfg.xml中的映射资源值,如下所示:

"classpath:UserDetails.hbm.xml“

代码语言:javascript
复制
<mapping resource="classpath:UserDetails.hbm.xml" />

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47868975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档