Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
vardeep kaur
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi use code tags..
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the pckg of your Pojo class..
And package of your mapping class.
Post your pojo here..
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
VivekSingh SinghVivek , please check your private messages.
 
Christian Dillinger
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using Hibernate Annotations or do you configure POJO via hbm.xml?
 
vardeep kaur
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No its not working

can there be some other problem???
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your help.
My problem is solved
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic