1

I know this question is asked quite often.

I get the below mapping error.

I have tried some permutations and combinations.
I've tried some solutions and cannot seem to get this fixed.

Can someone look at my below configuration?

The xmls are valid.
I use hibernate 3 and mysql as the database

Error Log:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/sample/Employee.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.sample.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:17)
    at com.sample.TestEmployee.main(TestEmployee.java:13)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/sample/Employee.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:523)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1511)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1479)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1458)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1432)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1352)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1338)
    at com.sample.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:12)
    ... 1 more
Caused by: org.hibernate.MappingException: invalid mapping
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:463)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:520)
    ... 8 more
Caused by: org.xml.sax.SAXParseException: The content of element type "id" must match "(meta*,column*,type?,generator?)".
    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.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.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:460)
    ... 9 more

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.sample.Employee" table="employee">
   <id name="idEmployee" column="ID">  
        <column name="uid" not-null="true"/>
        <generator class="native" />         
   </id>
   <property name="empName">
       <column name="empName" length="16" not-null="true" />
   </property>
   <property name="sex">
        <column name="sex" length="16" not-null="true" />
   </property>
</class>
</hibernate-mapping>

Employee class:
package com.sample;
public class Employee {

    private Long idEmployee;
    private String empName;
    private String sex;
    public Long getIdEmployee() {
        return idEmployee;
    }
    public void setIdEmployee(Long idEmployee) {
        this.idEmployee = idEmployee;
    }
    public String getEmpName() {
        return empName;
    }
    private String address;
    private int age;


    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
CREATE TABLE `employee` (
`idEmployee` int(11) NOT NULL AUTO_INCREMENT,
`empName` varchar(45) NOT NULL,
`sex` varchar(45) NOT NULL,
`age` int(11) DEFAULT NULL,
`address` varchar(45) DEFAULT NULL,
 PRIMARY KEY (`idEmployee`)
 );
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
BVAD
  • 75
  • 2
  • 6
  • Unrelated, but please find another tutorial to learn Hibernate. You're using it like we did more than 10 years ago, when annotations didn't exist. Nowadays, annotations are used to define the mapping. XML is obsolete. – JB Nizet Apr 19 '15 at 12:34
  • sure. will check the latest tutorial :) hey now I know how it was done before ;) thanks!!! – BVAD Apr 20 '15 at 14:29

2 Answers2

0

I think you miss the mapping for the attributes "address" and "age". Why you didn't make them ? And check the lengths tags you are using , why they are all equal to 16 ?
Try this :

<hibernate-mapping>
<class name="com.sample.Employee" table="employee">
   <id name="idEmployee" column="ID">  
        <generator class="native" />         
   </id>
   <property name="empName" column="empName"></property>
   <property name="sex" column="sex"></property>
   <property name="address" column="address"></property>
   <property name="age" column="age"></property>
</class>
</hibernate-mapping>
YANKEE
  • 59
  • 1
  • 2
0

Exception says:

Caused by: org.xml.sax.SAXParseException: The content of element type "id" must match "(meta*,column*,type?,generator?)".

Your ID type is mismatched. On Employee class your idEmployee variable type is Long and on .hbm.xml file your ID property mapping has native generator that means the ID is Integer type by default. So you should use type="long" annotation on your ID property. Or just try to use different type of ID generator. And on your ID annotation in .hbm.xml file you using two names for ID column. You are using column="ID" and column="uid". Example how .hbm.xml file should look like:

<hibernate-mapping>
     <class name="com.sample.Employee" table="employee">
        <id name="idEmployee" type="long">
            <column name="uid" not-null="true"/>
            <generator class="native" />
        </id>
        <property name="empName">
            <column name="empName" length="16" not-null="true" />
        </property>
        <property name="sex">
            <column name="sex" length="16" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

Or try to use SequenceStyleGenerator. Generator which creates separate ID sequence for every table by given sequence name. The default type of generated id is Long. .hbm.xml file example:

 <hibernate-mapping>
    <class name="com.sample.Employee" table="employee">
        <id name="idEmployee" column="ID">
            <generator class="org.hibernate.id.enhanced.SequenceStyleGenerator">
                <param name="optimizer">none</param>
                <param name="increment_size">1</param>
                <param name="sequence_name">seq_employee_id</param>
            </generator>
        </id>
        <property name="empName">
            <column name="empName" length="16" not-null="true" />
        </property>
        <property name="sex">
            <column name="sex" length="16" not-null="true" />
        </property>
    </class>
 </hibernate-mapping>

I hope it helps you... ☺

Dumbo
  • 1,630
  • 18
  • 33