2013-08-29 67 views
0

這裏是我的Employee.hbm.xml無法創建SessionFactory的object.org.hibernate.InvalidMappingException:無法從資源解析映射文檔Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping package = "com.demo.hibernate.beans"> 
    <class name="Employee" table="employee"> 
     <meta attribute="class-description"> 
     This class contains the employee detail. 
     </meta> 
     <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> 

在運行休眠我得到以下異常

Failed to create sessionFactory object.org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml 
Exception in thread "main" java.lang.ExceptionInInitializerError 
    at com.demo.hibernate.beans.ManageEmployee.main(ManageEmployee.java:20) 
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml 
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:575) 
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1593) 
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1561) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1540) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1514) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1434) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1420) 
    at com.demo.hibernate.beans.ManageEmployee.main(ManageEmployee.java:17) 
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream 
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:514) 
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:572) 
    ... 7 more 
Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org 
    at org.dom4j.io.SAXReader.read(SAXReader.java:484) 
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:505) 
    ... 8 more 

是一個什麼可能的原因是什麼?

編輯:發佈員工豆

public class Employee { 
     private int id; 
     private String firstName; 
     private String lastName; 
     private int 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; 
     } 
    } 

和Postgres的

create table EMPLOYEE (
    id INT NOT NULL DEFAULT nextval('emp_id_seq'::regclass), 
    first_name VARCHAR(20) default NULL, 
    last_name VARCHAR(20) default NULL, 
    salary  INT default NULL, 
    PRIMARY KEY (id) 
); 


CREATE SEQUENCE emp_id_seq 
    INCREMENT 1 
    MINVALUE 1 
    MAXVALUE 9223372036854775807 
    START 10000 
    CACHE 1; 
+0

也發表您的員工豆科和表結構。 – commit

回答

3

你的文檔類型有問題的數據庫結構。從你複製的地方?我複製了你的代碼並改變了docttype。有用。

使用此對Hibernate的DOCTYPE 3.0

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
相關問題