2013-01-10 51 views
0

這是我第一篇文章在stackoverflow: 我是新來的hibernate,Infact下面是我的第一個代碼在休眠: 我唾棄「無法解析映射文檔」錯誤,何時我跑我的主班。下面是 是你引用的代碼,請幫我解決這個問題。無法解析映射文檔在休眠的錯誤

配置文件:

<?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="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property> 
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property> 
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample</property> 
    <property name="hibernate.connection.username">one</property> 
    <property name="hibernate.connection.password">two</property> 
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> 
    <property name="hibernate.current_session_context_class">thread</property> 

    <mapping resource="EmployeeMapping.hbm.xml"/> 

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

映射文件:

<?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="Employee" table="EMPLOYEE"/> 
    <id name="emp_id" type="int" column="emp_id"/> 

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

    <property name="emp_branch"> 
     <column name="emp_branch"/> 
    </property> 
    <property name="emp_Salary"> 
     <column name="emp_salary"/> 
    </property> 
    <property name="emp_fav_dgt"> 
     <column name="emp_fav_dgt"/> 
    </property> 

</hibernate-mapping> 

POJO類:

public class Employee { 

    private int emp_id; 
    private String emp_name; 
    private String emp_branch; 
    private int emp_Salary; 
    private int emp_fav_dgt; 

    public int getEmp_Salary() { 
     return emp_Salary; 
    } 

    public void setEmp_Salary(int emp_Salary) { 
     this.emp_Salary = emp_Salary; 
    } 

    public String getEmp_branch() { 
     return emp_branch; 
    } 

    public void setEmp_branch(String emp_branch) { 
     this.emp_branch = emp_branch; 
    } 

    public int getEmp_fav_dgt() { 
     return emp_fav_dgt; 
    } 

    public void setEmp_fav_dgt(int emp_fav_dgt) { 
     this.emp_fav_dgt = emp_fav_dgt; 
    } 

    public int getEmp_id() { 
     return emp_id; 
    } 

    public void setEmp_id(int emp_id) { 
     this.emp_id = emp_id; 
    } 

    public String getEmp_name() { 
     return emp_name; 
    } 

    public void setEmp_name(String emp_name) { 
     this.emp_name = emp_name; 
    } 
} 

主類:

public class Main { 
    public static void main(String[] args) { 
     System.out.println("asdf"); 
     SessionFactory sessionFactory = null; 
     Transaction ts = null; 
     Session session = null; 
     Configuration conf = null; 
     try{ 
      conf = new org.hibernate.cfg.Configuration().configure(); 
      sessionFactory = conf.buildSessionFactory(); 
      session =sessionFactory.openSession(); 
      Employee customer = new Employee(); 
      customer.setEmp_id(1); 
      customer.setEmp_name("one"); 
      customer.setEmp_branch("old"); 
      customer.setEmp_Salary(1000); 
      customer.setEmp_fav_dgt(2); 
      session.save(customer); 
      ts.commit(); 
     } 
     catch(Exception e){ 
      e.printStackTrace();; 
     } 
     finally{ 
      session.close(); 
     } 

    } 
} 

回答

0

與字段相對應的屬性標籤應嵌套在類標籤內。

<class name="Employee" table="EMPLOYEE"> 
    <id name="emp_id" type="int" column="emp_id"/> 

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

    <property name="emp_branch"> 
     <column name="emp_branch"/> 
    </property> 
    <property name="emp_Salary"> 
     <column name="emp_salary"/> 
    </property> 
    <property name="emp_fav_dgt"> 
     <column name="emp_fav_dgt"/> 
    </property> 
</class>