Hi,
I try to perfom basic CURD operations in hibernate

"org.hibernate.InvalidMappingException" this is the error i am getting
help me to fix it.

i am using eclipse indigo, mysql

lib jar files are:

hibernate 3.jar, asm.jar,cglib - 2.1.jar, commons-logging - 1.0.4.jar,
commons-collections-2.1.1.jar, dom4j-1.6.jar, ehcache-1.1.jar, jta.jar,
log4j-1.2.9.jar, mysql-connector-java-5.0.8-bin.jar

Employee.java

package com.ganga.hibernate;
import java.util.Date;
public class Employee {
private int empno;
private int deptno;
private String ename;
private String job;
private double sal;
private Date hiredate;
public int getEmpno() {
    return empno;
}
public void setEmpno(int empno) {
    this.empno = empno;
}
public int getDeptno() {
    return deptno;
}
public void setDeptno(int deptno) {
    this.deptno = deptno;
}
public String getEname() {
    return ename;
}
public void setEname(String ename) {
    this.ename = ename;
}
public String getJob() {
    return job;
}
public void setJob(String job) {
    this.job = job;
}
public double getSal() {
    return sal;
}
public void setSal(double sal) {
    this.sal = sal;
}
public Date getHiredate() {
    return hiredate;
}
public void setHiredate(Date hiredate) {
    this.hiredate = hiredate;
}

}




HibernateBasicTest.java

package com.hibernate.test;
import com.ganga.hibernate.Employee;
import java.util.Date;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;

public class HibernateBasicTest {
    private SessionFactory factory;
    public void createEmployee(Employee emp){
        Session session=null;
        Transaction tx = null;
        try{
            session = factory.openSession();
            tx=session.beginTransaction();
            session.save(emp);
            tx.commit();
            System.out.println("Employee Created with Empno : "+emp.getEmpno());
        }
        catch(HibernateException hb){
            tx.rollback();
            System.out.println("Exception in creating Employee");
        }
        finally{
            session.close();
        }
    }

    public void updateEmployee(Employee emp){
        Session session=null;
        Transaction tx = null;
        try{
            session = factory.openSession();
            tx=session.beginTransaction();
            session.save(emp);
            tx.commit();
            System.out.println("Employee Updated ");
        }
        catch(HibernateException hb){
            tx.rollback();
            System.out.println("Exception in updating Employee");
        }
        finally{
            session.close();
        }
    }

    public Employee getEmployee(int empno){
        Session session=null;

        try{
            session = factory.openSession();
            Employee emp = (Employee)session.load(Employee.class,new Integer(empno));
            return emp;
            }
        catch(HibernateException hb){
            System.out.println("Exception in loading Employee");
            return null;
        }
        finally{
            session.close();
        }
    }

    public void removeEmployee(Employee emp){
        Session session=null;
        Transaction tx = null;
        try{
            session = factory.openSession();
            tx=session.beginTransaction();
            session.delete(emp);
            tx.commit();
            System.out.println("Employee removed ");
        }
        catch(HibernateException hb){
            tx.rollback();
            System.out.println("Exception in removing Employee");
        }
        finally{
            session.close();
        }
    }
    public static void main(String[] args) {
        Logger.getRootLogger().setLevel(Level.OFF);
        HibernateBasicTest test = new HibernateBasicTest();
        //preparing configuration object
        Configuration cfg = new Configuration();
        cfg.configure();
        test.factory = cfg.buildSessionFactory();

        Employee emp = new Employee();
        emp.setEname("Mark");
        emp.setSal(500);
        emp.setJob("Clerk");
        emp.setHiredate(new Date());
        emp.setDeptno(20);
        System.out.println();
        //updating
        emp.setJob("Manager");
        emp.setSal(1000);

        //Getting an employee
        emp = test.getEmployee(Integer.parseInt(args[0]));
        if(emp!=null){
            System.out.println("Employees Details of Empno " + emp.getEmpno());
            System.out.println("Name : "+ emp.getEname());
            System.out.println("Salary : "+ emp.getSal());
            System.out.println("HireDate : "+ emp.getHiredate());
        }
        System.out.println();
        //removing an employee
        if(emp!=null)
            test.removeEmployee(emp);
    }

}




hibernate.cfg.xml

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

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/ganga</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>


        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

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

    </session-factory>

</hibernate-configuration>



This is the error i am getting

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.
Exception in thread "main" 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.hibernate.test.HibernateBasicTest.main(HibernateBasicTest.java:89)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:549)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:613)
    ... 7 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 19; Document root element "hibrnate-mapping", must match DOCTYPE root "hibernate-mapping".
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.rootElementSpecified(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:546)
    ... 8 more




Employee.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibrnate-mapping>
<class name="com.ganga.hibernate.Employee" table="emp" lazy="false">
<id name = "empno" type="int">
<column name="empno"/>
<generator class="increment"/>
</id>

<property name="ename" column="ename"/>
<property name="sal"/>
<property name="job"/>
<property name="hiredate"/>
<property name="deptno"/>
</class>
</hibrnate-mapping>

easiest would be to get rid of the xml mapping and use JPA2 compatible annotation based mapping instead.

fine its good,
but i want to gain some knowledge in xml mapping
can any one help me to fix this error

thanks for your time

the error is clear, your xml is invalid because of a typo on line 4.
you typed 'hibrnate-mapping' where it should read 'hibernate-mapping'.

<mapping resource="Employee.hbm.xml"/> in hibernate.cfg.xml

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.