Home Hibernate Hibernate4 org.hibernate.mappingexception
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

org.hibernate.mappingexception

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

org.hibernate.mappingexception

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(
"net/roseindia/Worker.hbm.xml").addResource(
"net/roseindia/WorkerDetail.hbm.xml");
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
sf = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
System.out
.println("Example : Hibernate One to One Mapping using Xml ");
Session session = sf.openSession();
session.beginTransaction();

// 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) {
e.printStackTrace();
}

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

session.save(worker);

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

session.getTransaction().commit();
session.close();

}
}

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"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping >

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

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

</class>
</hibernate-mapping>

WorkerDetail.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<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>
</generator>
</id>
<one-to-one name="worker" class="net.roseindia.Worker"
constrained="true"></one-to-one>

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

</hibernate-mapping>

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


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


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


Caused by: java.lang.ClassNotFoundException: Worker
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:228)
... 11 more

CAUSE

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.

Advertisements

Liked it!  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.

Posted on: April 13, 2012

Ask Questions?    Discuss: org.hibernate.mappingexception  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com