2013-08-06 51 views
0

我試着寫與Hibernate和MySQL的簡單PROGRAMM,但我得到這個錯誤:映射文件未找到錯誤

INFO: Configuration resource: /hibernate.cfg.xml 
Aug 06, 2013 3:51:37 PM org.hibernate.cfg.Configuration addResource 
INFO: Reading mappings from resource : /hibernate-contact.hbm.xml 
resource: /hibernate-contact.hbm.xml not found 
BUILD SUCCESSFUL (total time: 0 seconds) 

這是我的映射文件:

<?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> 
    <class name="Untersuchungsraum.Contact" table="contact"/> 
    <id column="firstname" type="string" > 
     <generator class="native"> 
     </generator> 
    </id> 
    <property name="firstname"> 
     <column name="firstname"></column> 
    </property> 

    <property name="lastnamename"> 
     <column name="lastname"></column> 
    </property> 

    <property name="email"> 
     <column name="email"></column> 
    </property> 


... 

,這是我的XML配置文件:

<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/rcf?zeroDateTimeBehavior=convertToNull</property> 
    <property name="hibernate.connection.username">root</property> 
    <mapping resource="/hibernate-contact.hbm.xml"/> 
    <mapping/> 
    </session-factory> 
</hibernate-configuration> 

的我的項目是這樣的: enter image description here

爲什麼我會得到這個問題,我能解決嗎?

編輯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> 
    <class name="Untersuchungsraum.Contact" table="contact"> 
    <property name="firstname"> 
     <column name="Firstname"></column> 
    </property> 

    <property name="lastname"> 
     <column name="lastname"></column> 
    </property> 

    <property name="email"> 
     <column name="email"></column> 
    </property> 
    </class> 
    </hibernate-mapping> 

和我的POJO類:

package Untersuchungsraum; 
public class Contact { 
    private String email; 
    private String firstname; 
    private String lastname; 

    public String getEmail() { 
     return email; 
    } 
    public void setEmail(String email) { 
     this.email = email; 
    } 
    public String getFirstname() { 
     return firstname; 
    } 
    public void setFirstname(String firstname) { 
     this.firstname = firstname; 
    } 
    public String getLastname() { 
     return lastname; 
    } 
    public void setLastname(String lastname) { 
     this.lastname = lastname; 
    } 

}

+0

讓你的生活變得簡單,並把它放在一些包裏。 –

+0

我做到了,但錯誤仍然 – TangoStar

+0

刪除斜槓並嘗試.. – commit

回答

0

嘗試把.xml.hbm.xmlsrc文件夾,而不是這個包。

+0

我做到了,但也有問題:'Aug 06,2013 4:15:57 PM org.hibernate.cfg.Configuration configure 信息:配置來自資源:/hibernate.cfg.xml Aug 06,2013 4:15:57 PM org.hibernate.cfg.Configuration getConfigurationInputStream 信息:配置資源:/hibernate.cfg.xml BUILD SUCCESSFUL(總時間:0秒) ' – TangoStar

+0

然後把'.xml'和'.hbm.xml'放在包的外面,就是直接放在'src'文件夾中。 – user2550754

+0

現在我有另一個錯誤:'嚴重:XML解析錯誤:XML InputStream(33)元素類型「hibernate-mapping」的內容必須匹配「(meta *,typedef *,import *,(class | subclass | joined-subclass |工會子類)*,結果集*(查詢| SQL查詢)*,過濾器的高清*,數據庫對象*)」。 **無法從資源hibernate-contact.hbm.xml中解析映射文檔**' – TangoStar