org.hibernate.mappingexception

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


 

Ads

Tutorials   
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
Ads

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

Share on Google+Share on Google+

org.hibernate.mappingexception

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

Advertisements

 

Discuss: org.hibernate.mappingexception  

Post your Comment


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

Ads

 

Ads