Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training


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


How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code


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.


Share on Google+Share on Google+


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



Discuss: org.hibernate.mappingexception  

Post your Comment

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