File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Beginner in hibernate getting error : org.hibernate.MappingException: Unknown entity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Beginner in hibernate getting error : org.hibernate.MappingException: Unknown entity" Watch "Beginner in hibernate getting error : org.hibernate.MappingException: Unknown entity" New topic
Author

Beginner in hibernate getting error : org.hibernate.MappingException: Unknown entity

vardeep kaur
Greenhorn

Joined: Dec 30, 2008
Posts: 11
Hi

I am getting org.hibernate.MappingException: Unknown entity error
I have class named "FirstExample". Its code is as follows:

[color=cyan]package com;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class FirstExample {

public static void main(String[] args) {
Session session = null;

try {
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
System.out.println("session is connected " + session.isConnected());
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(2);
contact.setFirstName("Raj");
contact.setLastName("S");
contact.setEmail("raj.s@yahoo.com");
session.save(contact);
System.out.println("Done");
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}
}
[/color]

The Code of hibernate.cfg.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://10.10.10.19:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">independent</property>
<!-- Mapping files -->
<mapping package="com"/>
<mapping class="com.Contact"/>
</session-factory>
</hibernate-configuration>

Can you please tell me where is the problem?



Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Hi use code tags..
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
vardeep kaur wrote:Hi

I am getting org.hibernate.MappingException: Unknown entity error
I have class named "FirstExample". Its code is as follows:

[color=cyan]package com;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class FirstExample {

public static void main(String[] args) {
Session session = null;

try {
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
System.out.println("session is connected " + session.isConnected());
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(2);
contact.setFirstName("Raj");
contact.setLastName("S");
contact.setEmail("raj.s@yahoo.com");
session.save(contact);
System.out.println("Done");
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}
}
[/color]

The Code of hibernate.cfg.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://10.10.10.19:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">independent</property>
<!-- Mapping files -->
<mapping package="com"/>
<mapping class="com.Contact"/>
</session-factory>
</hibernate-configuration>

Can you please tell me where is the problem?




use this <mapping resource="Contact.hbm.xml"/>

Post you mapping file also
vardeep kaur
Greenhorn

Joined: Dec 30, 2008
Posts: 11
Thanks for the reply

I tried with following mapping also

<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>

But again its throwing same error.
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
What is the pckg of your Pojo class..
And package of your mapping class.
Post your pojo here..
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Post your mapping File...
And pckg of all the files...
If Cfg.xml and mapping file is same pckg then use only
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

VivekSingh SinghVivek , please check your private messages.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 189
Are you using Hibernate Annotations or do you configure POJO via hbm.xml?
vardeep kaur
Greenhorn

Joined: Dec 30, 2008
Posts: 11
Cfg.xml and mapping file are in the same default package.
Package of my POJO class is "com"

my mapping file "contact.hbm.xml" is as follows:


<?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="com.Contact" table="CONTACT">
<id column="ID" name="id" type="int">
<generator class="assigned"/>
</id>
<property name="firstName">
<column name="FIRSTNAME"/>
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
</class>
</hibernate-mapping>

Thanks
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
vardeep kaur wrote:
<mapping package="com"/>
<mapping class="com.Contact"/>


replace this with

And give a try..I have made a similar configuration and its working for me.
vardeep kaur
Greenhorn

Joined: Dec 30, 2008
Posts: 11
No its not working

can there be some other problem???
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Please use code tags vardeep. It makes it way easier to read your code.


Cfg.xml and mapping file are in the same default package.
Package of my POJO class is "com"

This sounds odd. Normally your class file and corresponding mapping file would exist in the same package, and so in the same directory.
vardeep kaur
Greenhorn

Joined: Dec 30, 2008
Posts: 11
Thanks a lot for your help.
My problem is solved
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginner in hibernate getting error : org.hibernate.MappingException: Unknown entity