2013-03-20 9 views
1

得到下面的錯誤在Hibernate中,我在運行時收到以下錯誤在Hibernate中,我在運行時

1 employee.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 ="com.hibernate.basic"> 



    <class name="Employee" table="Employee" lazy="false"> 

     <id name="id" column="EMPID" type ="int"> 

      <generator class="increment"></generator> 
     </id> 

     <property name="firstName" column="NAME"></property> 
     <property name="lastName"column="LNAME"></property> 

    </class> 

2. 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> 
     <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> 
     <property name="connection.url">jdbc:oracle:thin:@172.16.3.94:1521:EAMABP</property> 
     <property name="connection.username">EAM</property> 
     <property name="connection.password">EAM</property> 
     <property name="hibernate.connection.pool_size">10</property> 
     <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 
     <property name="current_session_context_class">thread</property> 
     <property name="hibernate.show_sql">true</property> 
     <property name="hibernate.show_sql">true</property> 
     <property name="hibernate.format_sql">true</property> 
     <property name="hibernate.hbm2ddl.auto">update</property> 
     <mapping resource="employee.hbm.xml" /> 
    </session-factory> 

</hibernate-configuration> 

3 Employee.java

package com.hibernate.basic; 

public class Employee { 

    private int id; 
    private String FName, LName; 

    public String getFName() { 

     return FName; 
    } 

    public void setFName(String FName) { 

     this.FName = FName; 
    } 

    public String getLName() { 
     return LName; 
    } 

    public void setLName(String LName) { 

     this.LName = LName; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 
} 

4.執行後StoreData.java

package com.hibernate.basic; 

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

public class StoreData { 
    public static void main(String[] args) { 

     // creating configuration object 
     Configuration cfg = new Configuration(); 
     cfg.configure("hibernate.cfg.xml");// populates the data of the 
              // configuration file 

     // creating seession factory object 
     SessionFactory factory = cfg.buildSessionFactory(); 

     // creating session object 
     Session session = factory.openSession(); 

     // creating transaction object 
     Transaction t = session.beginTransaction(); 

     Employee e1 = new Employee(); 
     e1.setId(115); 
     e1.setFName("sonoo"); 
     e1.setLName("jaiswal"); 

     session.persist(e1);// persisting the object 

     t.commit();// transaction is commited 
     session.close(); 

     System.out.println("successfully saved"); 

    } 
} 

發生錯誤:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource employee.hbm.xml at org.hibernate.cfg.Configuration.addResource(Configuration.java:569) at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508) at org.hibernate.cfg.Configuration.configure(Configuration.java:1428) at com.hibernate.basic.StoreData.main(StoreData.java:13) Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508) at org.hibernate.cfg.Configuration.addResource(Configuration.java:566) ... 6 more Caused by: org.dom4j.DocumentException: Error on line 14 of document : Element type "property" must be followed by either attribute specifications, ">" or "/>". Nested exception: Element type "property" must be followed by either attribute specifications, ">" or "/>". at org.dom4j.io.SAXReader.read(SAXReader.java:482) at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499) ... 7 more

回答

0

錯誤中明確規定的行號和error.Having一看,這可能幫幫我。

文檔行14上的錯誤:元素類型「屬性」後面必須跟有屬性規範「>」或「/>」。嵌套異常:元素類型「屬性」後面必須有屬性規範,「>」或「/>」。

+0

很抱歉,但它不是解決problem..I我得到一些其他錯誤 – 2013-03-21 07:56:46

+0

謝謝,但你問我修改罐子代碼這是不準確的issue..DOM解析器正試圖解析該文件。 – 2013-03-25 06:30:48

3

name="lastName"column之間有空位。

+0

謝謝,但在此屬性的Java空間不影響。 – 2013-03-25 06:29:41

0

這個錯誤通常是因爲你的標籤沒有關閉而發生的。如果你在這裏發佈了什麼,我想你還沒有關閉employee.hbm中的hibernate-mapping標籤。試試吧! 希望這有助於!

+0

謝謝主席先生,我已經關閉的3天即標籤previously.After戰鬥,我學會了三件事,請記下這1.如果您定義生成標記爲增量則評論setID.2.In Java類和映射hibernate.hbm.xml屬性名稱應該完全相同(這是我的錯誤)。 3.應該在hibernate.cfg.xml中非常精確地給出xml的路徑.......... – 2013-03-25 06:36:37

0

我有同樣的錯誤:

Element type "property" must be followed by either attribute specifications, ">" or "/>". Nested exception: Element type "property" must be followed by either attribute specifications, ">" or "/>".

而且我固定它改變該行以*的.hbm.xml文件

有柱=「ESAMBIGUA之間缺少空間「和長度=」1「/>

第一條評論,從Eduard Wirch真的幫了我。謝謝!

相關問題