2012-11-07 51 views
1

我是新的T冬眠 當我試圖執行下面的代碼我得到以下異常:重複導入:員工 - >員工org.hibernate.InvalidMappingException:無法從資源解析映射文檔Employee.hbm.xml

org.hibernate.InvalidMappingException:無法從資源Employee.hbm.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/hibernatetest 
    </property> 
    <property name="hibernate.connection.username"> 
    root 
    </property> 
    <property name="hibernate.connection.password"> 
    root 
    </property> 

    <!-- List of XML mapping files --> 
    <mapping resource="Employee.hbm.xml"/> 

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

來源:

package com.practice.HBtest; 

public class Employee { 

    private int id; 
    private String firstName; 
    private String lastName; 
    private Integer salary; 

    public Employee() {} 

    public Employee(String fname, String lname, int salary) { 
     this.firstName = fname; 
     this.lastName = lname; 
     this.salary = salary; 
    } 

    public int getId() { 
     return id; 
    } 

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

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String first_name) { 
     this.firstName = first_name; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String last_name) { 
     this.lastName = last_name; 
    } 

    public int getSalary() { 
     return salary; 
    } 

    public void setSalary(int salary) { 
     this.salary = salary; 
    } 
} 

映射:

<hibernate-mapping> 
<class name="Employee" table="EMPLOYEE"> 
<id name="id" type="int" column="id"> 
<generator class="native"/> 
</id> 

<property name="firstname" column="first_name" type="String"/> 
<property name="lastname" column="last_name" type="String"/> 
<property name="salary" column="salary" type="int"/>  
</class> 
</hibernate-mapping> 

來源提取物:

import org.hibernate.HibernateException; 
import org.hibernate.SessionFactory; 
import org.hibernate.Session; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 

public class EmployeeRecordManager { 

    private static SessionFactory sessionFactory; 
    private static ServiceRegistry serviceRegistry; 
    private Session session; 

    public static void main(String[] args) { 

     EmployeeRecordManager erm = new EmployeeRecordManager(); 

     try { 
      Configuration cfg = new Configuration().addResource("Employee.hbm.xml"); 
      SessionFactory sessionFactory = cfg.configure().buildSessionFactory(serviceRegistry); 

     } catch (Exception e) { 
      System.out.println(e); 
     } 
     Integer empID1 = erm.addEmployee("Amruta", "Ali", 1000); 
     Integer empID2 = erm.addEmployee("Ruchi", "Das", 5000); 
     Integer empID3 = erm.addEmployee("Mitul", "Paul", 10000); 

     System.out.println(erm); 

    } 

    public Integer addEmployee(String fname, String lname, int salary) { 
     Session session = sessionFactory.openSession(); 
     Transaction tx = null; 
     Integer empID = null; 
     try { 
      tx = session.beginTransaction(); 
      Employee employee = new Employee(fname, lname, salary); 
      empID = (Integer) session.save(employee); 
      tx.commit(); 
     } catch (HibernateException e) { 
      if (tx != null) 
       tx.rollback(); 
      e.printStackTrace(); 
     } finally { 
      session.close(); 
     } 
     return empID; 
    } 

} 







    Nov 7, 2012 5:36:15 PM org.hibernate.annotations.common.Version <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 
Nov 7, 2012 5:36:15 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.1.8.Final} 
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml 
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml 
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration doConfigure 
INFO: HHH000041: Configured SessionFactory: null 
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration$MappingsImpl addImport 
INFO: HHH000071: Duplicate import: Employee -> Employee 
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3417) 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3406) 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3394) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) 
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:24) 
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping Employee 
    at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2582) 
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:174) 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3414) 
    ... 5 more 
Exception in thread "main" java.lang.NullPointerException 
    at com.practice.HBtest.EmployeeRecordManager.addEmployee(EmployeeRecordManager.java:39) 
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:30) 
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml 
After Adding the code mentioned by you was getting below Exception: 


    Nov 7, 2012 6:48:24 PM org.hibernate.annotations.common.Version <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 
Nov 7, 2012 6:48:24 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.1.8.Final} 
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml 
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: hibernate.cfg.xml 
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml 
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration doConfigure 
INFO: HHH000041: Configured SessionFactory: null 
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml 
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration doConfigure 
INFO: HHH000041: Configured SessionFactory: null 
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration$MappingsImpl addImport 
INFO: HHH000071: Duplicate import: Employee -> Employee 
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3417) 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3406) 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3394) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) 
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:24) 
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping Employee 
    at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2582) 
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:174) 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3414) 
    ... 5 more 
Exception in thread "main" java.lang.NullPointerException 
    at com.practice.HBtest.EmployeeRecordManager.addEmployee(EmployeeRecordManager.java:39) 
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:30) 
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml 
+2

發表您的'Employee.hbm.xml'代碼 – Abubakkar

+0

一些問題在'Employee.hbm.xml'或它的路徑。 –

+0

[Duplicated](http://stackoverflow.com/questions/12010056/org-hibernate-invalidmappingexceptioncould-not-parse-mapping-document-from-reso)參考鏈接進行配置更改 – gks

回答

2

還有就是你的hbm.xml屬性名之間的差距外殼:

<property name="firstname" column="first_name" type="String"/> 
<property name="lastname" column="last_name" type="String"/> 
<property name="salary" column="salary" type="int"/> 

和你們班的實際成員字段:

private String first**N**ame; 
private String last**N**ame; 
private Integer salary; 

更重要的是你的getter和setter方法在你的班上也有駝峯規則,而Hibernate會尋找 getFirstname()setFirstname(),...

+0

我已經完成了上述變更,但仍然給我相同的例外。 – Amruta

+0

也繼續與例外它說:線程主異常:「java.lang.NullPointerException」 – Amruta

0

你能粘貼您的整個堆棧跟蹤?在沒有整個堆棧跟蹤的情況下進行調試很困難,因爲在載入休眠配置的時候會出現很多錯誤。

它可能與您定義配置&建立連接的方式有關。我最常做的是:

  • 負荷從主休眠配置文件中的配置 配置=新配置()配置(「hibernate.cfg.xml中」)。

  • 從您加載的配置中建立會話工廠 sessionFactory = configuration.buildSessionFactory();

沒有理由要明確添加的hbm.xml資源(如您已在主配置文件已經決定了資源,也沒有建立一個會話工廠時使用的服務註冊表參數...

而且你的ID映射可能需要把數據庫列在一個單獨的標籤,如:

<id name="id" type="int"> 
      <column name="id" scale="0" /> 
      <generator class="native" /> 
</id> 
+0

是的確定在這裏我是: – Amruta

+0

當我已經嘗試過你的建議加載配置從主休眠配置文件配置=新配置().configure( 「hibernate.cfg.xml中」);它會拋出一個異常 – Amruta

+0

任何人都可以幫助我嗎? – Amruta

0

我能解決這個問題 1。新增的對MySQL連接器JAR 2.Added正確的端口爲 3.Used以下配置:

Configuration configuration = new Configuration(); 
      configuration.configure(); 
      serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();   
      sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

感謝 大家誰這麼以往任何時候都作出了貢獻

0

我經歷過這個問題;發現以下解決方案

  • 清潔你的服務器構建目錄即乾淨的Eclipse項目,並 確保相同(dulpicate)

.hbm文件不應該在那裏。

  • 如果您的POJO在項目之外;然後保持到包裝和.hbm文件中定義包

  • 您必須擁有所有可用其getter和setter的POJO w.r.t HBM文件中的字段。

  • 確保字段區分大小寫,如上所述「Filip」。

  • 檢查jar文件都可以

相關問題