2013-03-25 114 views
1

您好,我得到異常的映射我的休眠: 初始SessionFactory的創建failed.org.hibernate.MappingNotFoundException:資源:bbstats /域/ User.hbm.xml沒有找到線索 異常「主要的」 java .lang.ExceptionInInitializerError爲什麼我得到MappingNotFound異常

這裏是我的hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?> 
     <!DOCTYPE hibernate-configuration PUBLIC 
       "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
       "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://sql10.lh.pl:3306/zamocno_bb</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password"></property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

     <!-- Enable Hibernate's automatic session context management --> 
     <property name="current_session_context_class">thread</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">false</property> 

     <property name="hbm2ddl.auto">validate</property> 

     <mapping resource="bbstats/domain/User.hbm.xml"/> 


    </session-factory> 
</hibernate-configuration> 

這裏是我的User.hbm.xml:

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

<hibernate-mapping package="bbstats.domain"> 

    <class name="bbstats.domain.Users" table="USERS"> 
     <id name="id" column="ID"> 
      <generator class="native"/> 
     </id> 
     <property name="title" column="nick"/> 
    </class> 

</hibernate-mapping> 

這裏Users.class:

package bbstats.domain; 

/** 
* Created with IntelliJ IDEA. 
* User: Rafał 
* Date: 25.03.13 
* Time: 13:33 
* To change this template use File | Settings | File Templates. 
*/ 
public class Users 
{ 
    private Long id; 
    private String title; 

    public Users() {} 

    public Users(Long id, String title) { 
     this.id = id; 
     this.title = title; 
    } 

    public Long getId() { 
     return id; 
    } 

    private void setId(Long id) { 
     this.id = id; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 
} 

和HibernateUtil.class:目錄

package bbstats.util; 

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

public class HibernateUtil 
{ 

    private static final SessionFactory sessionFactory = buildSessionFactory(); 

    private static SessionFactory buildSessionFactory() { 
     try { 
      return new Configuration() 
        .configure() 
        .buildSessionFactory(); 
     } catch (Throwable ex) { 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 
} 

結構: enter image description here

+1

試試你的映射文件移動到正確的文件夾結構下的src /主/資源。 – NilsH 2013-03-25 22:10:32

回答

1

問題是在你的User.hbm.xml

Here

<class name="bbstats.domain.Users" table="USERS"> 
     <id name="id" column="ID"> 
      <generator class="native"/> 
     </id> 
     <property name="title" column="nick"/> 
    </class> 

刪除您的類的全名,因爲你已經在這個<hibernate-mapping package="bbstats.domain">線提供類的包名

更新您的User.hbm.xml與下面的代碼

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

<hibernate-mapping package="bbstats.domain"> 

    <class name="Users" table="USERS"> 
     <id name="id" column="ID"> 
      <generator class="native"/> 
     </id> 
     <property name="title" column="nick"/> 
    </class> 

</hibernate-mapping> 

注意: 你pojo的名字是Users.java我建議你請根據您的域對象重命名您的User.hbm.xmlUsers.hbm.xml,以便閱讀。

1

請確保在Eclipse中Java構建路徑必須反映* / .xml。 右鍵單擊項目>屬性> Java構建路徑>源>添加* /的.xml (Source文件夾構建路徑,包括**。XML)

+0

請注意添加建議:) – 2013-09-09 19:03:26

相關問題