H2 DB – Hibernate示例 – 无法解析资源中的映射文档

H2 DB — Hibernate Example — Could not parse mapping document from resource

*以下每个文件都在同一位置*错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    SLF4J: Failed to load class"org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
org.hibernate.InvalidMappingException: Could not parse mapping document from resource ./employee.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:616)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1635)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
    at com.yahoo.hibernatelearning.FirstExample.main(FirstExample.java:19)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:555)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:613)
    ... 7 more
Caused by: org.dom4j.DocumentException: http://hibernate.sourceforge.net/%0Ahibernate-mapping-3.0.dtd Nested exception: http://hibernate.sourceforge.net/%0Ahibernate-mapping-3.0.dtd
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:546)
    ... 8 more
Exception in thread"main" java.lang.NullPointerException
    at com.yahoo.hibernatelearning.FirstExample.main(FirstExample.java:33)

休眠配置:hibernate.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:./db/repository</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.default_schema">PUBLIC</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!--  Mapping files  -->
<mapping resource="./employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

号映射配置:employee.hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/
hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.yahoo.hibernatelearning.Employee" table="employee">
<id name="empId" type="int" column="emp_id">
<generator class="native"/>
</id>
<property name="empName">
<column name="emp_name" />
</property>
<property name="empSal">
<column name="emp_sal" />
</property>
</class>
</hibernate-mapping>

映射类:employee.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    package com.yahoo.hibernatelearning;

    public class Employee {

        private int empId;
        private String empName;
        private int empSal;

        public int getEmpId() {
            return empId;
        }  

        public void setEmpId(int empId) {
            this.empId = empId;
        }

        public String getEmpName() {
            return empName;
        }

        public void setEmpName(String empName) {
        this.empName = empName;
        }

        public int getEmpSal() {
            return empSal;
        }

        public void setEmpSal(int empSal) {
            this.empSal = empSal;
        }

    }

。代码:firstexample.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    package com.yahoo.hibernatelearning;

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

    public class FirstExample {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Session sess = null;
    Transaction tran = null;
    try{
    SessionFactory sessFact = new Configuration().configure().buildSessionFactory();
    sess = sessFact.openSession();
    System.out.println("Session:"+ sess);
    tran = sess.beginTransaction();
    Employee emp = new Employee();
    emp.setEmpName("Birendra Kumar");
    emp.setEmpSal(12000);
    sess.save(emp);
    tran.commit();
    }
    catch(Exception ex){
    ex.printStackTrace();
    }
    finally{
    sess.close();
    }

    }

    }

%0A表示问题在于http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd之间的线路馈送。

通过移除线路馈送来解决问题:

1
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"