2017-12-18 38 views
0

我正在使用hibernate。我创建了一个名为UserDetails(POJO类)的表,其中包含id和name。不过,我发现很难,因为它给我这个错误执行程序 -hibernate给我 - “主要”org.hibernate.MappingNotFoundException:资源:hibernate_hbm.xml.UserDetails.hbm.xml找不到

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] UserDetails-

package hibernate_hbm.xml; 


public class UserDetails { 


    private int id; 
    private String name; 

//setter & getters   

} 
包含的UserDetails对象和会话工厂

[2] 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> 
+0

如果在包“hibernate_hbm.xml”中有一个资源UserDetails.hbm.xml,它的资源路径应该是'hibernate_hbm/xml/UserDetails.hbm.xml'而不是'hibernate_hbm.xml.UserDetails.hbm.xml'(在'hibernate.cfg.xml'中) –

+0

@Alex我输入了 - 和hibernate.cfg.xml文件中的。它显示错误 - 引起:org.hibernate.MappingException:找到属性时找不到类hibernate_hbm.xml.UserDetails.hbm.xml id AND引发:java.lang.ClassNotFoundException:hibernate_hbm.xml.UserDetails.hbm。 xml –

回答

0

作为该文件是类路径上尝试在hibernate.cfg映射资源值.XML如下:

“的classpath:UserDetails.hbm.xml”

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

做共享的文件夹structu因为这将有助于确切的路径使用

+0

- >文件夹结构是:hibernate_hbm.xml >> src >> hibernate_hbm.xml> A.java> UserDetails.java> UserDetails.hbm.xml –

+0

请使用markdown标签格式化您的文件夹结构 –

+0

@Alex从Project> hibernate_hbm.xml> src> hibernate3> A.java&UserDetails.java&UserDetails.hbm.xml> hibernate.cfg.xml –