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

hibernate

* Each of below files are in same location *

Error :


    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 Config: hibernate.cfg.xml

<?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>

Mapping Config: employee.hbm.xml

<?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>

Mapping Class : Employee.java


    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;
        }

    }

Code: FirstExample.java


    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();
    }

    }

    }

Best Answer

%0A tells that problem is line feed between http://hibernate.sourceforge.net/ and hibernate-mapping-3.0.dtd.

Problems is solved by removing line feed:

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