Share on Google+Share on Google+


In this section, you will learn about org.hibernate.mappingexception and what is the root cause of this exception.


In this section, you will learn about org.hibernate.mappingexception and what is the root cause of this exception.

The root cause of org.hibernate.mappingexception is the mapping done in your mapped pojo class or mapped XML is not correct.

For example, given below example code of one-to-one mapping :

package net.roseindia;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class ManageWorker {
private static SessionFactory sf;
private static ServiceRegistry serviceRegistry;

public static void main(String[] args) {
try {
Configuration configuration = new Configuration().addResource(
serviceRegistry = new ServiceRegistryBuilder().applySettings(
sf = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
.println("Example : Hibernate One to One Mapping using Xml ");
Session session = sf.openSession();

// For passing Date of birth as String
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dob = null;
try {
dob = sdf.parse("1987-05-21");
} catch (ParseException e) {

Worker worker = new Worker("Sushmita", "Dasgupta", dob, "919595959595");
WorkerDetail workerDetail = new WorkerDetail("Lake Town", "Kolkata",
"West Bengal", "India");

List<Worker> workerList = session.createQuery("from Worker").list();
for (Worker work1 : workerList) {
System.out.println(work1.getFirstname() + " , "
+ work1.getLastname() + ", "
+ work1.getWorkerDetail().getState());



And the mapping in Worker.hbm.xml is given below :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<hibernate-mapping >

<class name="Worker" table="worker">
<id name="workerId" column="worker_id">
<generator class="native" />
<one-to-one name="workerDetail" class="net.roseindia.WorkerDetail"

<property name="firstname" column="firstname" />
<property name="lastname" column="lastname" />
<property name="birthDate" type="date" column="birth_date" />
<property name="cellphone" column="cell_phone" />



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<hibernate-mapping package="net.roseindia">

<class name="WorkerDetail" table="workerdetail">

<id name="workerId" type="java.lang.Long">
<column name="worker_id" />
<generator class="foreign">
<param name="property">worker</param>
<one-to-one name="worker" class="net.roseindia.Worker"

<property name="street" column="street" />
<property name="city" column="city" />
<property name="state" column="state" />
<property name="country" column="country" />


When you execute the above code, you will get the following error :

Failed to create sessionFactory Could not parse mapping document from resource net/roseindia/Worker.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at net.roseindia.ManageWorker.main(
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource net/roseindia/Worker.hbm.xml
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(
at org.hibernate.cfg.Configuration.secondPassCompile(
at org.hibernate.cfg.Configuration.buildSessionFactory(
at net.roseindia.ManageWorker.main(

Caused by: org.hibernate.MappingException: class Worker not found while looking for property: workerId
at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(
at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(
at org.hibernate.cfg.HbmBinder.bindSimpleId(
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(
at org.hibernate.cfg.HbmBinder.bindRootClass(
at org.hibernate.cfg.HbmBinder.bindRoot(
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(
... 5 more

Caused by: java.lang.ClassNotFoundException: Worker
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(
at org.hibernate.internal.util.ReflectHelper.classForName(
at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(
... 11 more


Since Hibernate throw org.hibernate.MappingException when there is an error in mapping file. Since you can see in the above case that this exception is thrown because it didn't able to find Worker class. This is because, in the Worker.hbm.xml , the package description is not included. It should be done like this :

<hibernate-mapping package="net.roseindia">

You can view the correct example with downloadable code here.


Posted on: April 13, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: org.hibernate.mappingexception  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image