• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" org.hibernate.MappingException: Unknown entity: Employee

 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI I am getting the below error when I try and run some code using HIBERNATE.

ERROR->" Exception in thread "main" org.hibernate.MappingException: Unknown entity: Employee"

I have given all the other details below.Please help me out of this error.

Below given my hibernate configuration file(hibernate.config.xml)

<?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="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedata</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>

<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
</session-factory>

</hibernate-configuration>


Here is my session factory code.

MysessionFactory.java


import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class MySessionFactory {

@SuppressWarnings("deprecation")
static Session getSession(){
Configuration conf = new Configuration();
conf.configure("hibernate.cfg.xml");
SessionFactory sf = conf.buildSessionFactory();
return sf.openSession();
}

}



My POJO class(employee.java)

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Employee{

@Id
private int empid;
private String empname;


public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}

}


And finally my client programme(ClientProgram.java)



import org.hibernate.Session;


public class ClientProgram {


public static void main(String[] args) {

Session session = MySessionFactory.getSession();
session.beginTransaction();
Employee emp =new Employee();
emp.setEmpid(1);
emp.setEmpname("aneek");
session.save(emp);

session.getTransaction().commit();

session.close();



}

}


But whenever I am trying to run this code I am getting the below given error.

"Exception in thread "main" org.hibernate.MappingException: Unknown entity: Employee"

 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

See this!
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any problem with the configuration file here?
 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that your xml mapper for Employee entity is not specified inside hibernate.cfg.xml
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now getting the below error...

WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
2 Jul, 2013 10:15:45 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com.hibernate007.pkg.Employee.hbm.xml
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.hibernate007.pkg.Employee.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:738)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2167)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2139)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2119)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2072)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1987) .
at com.hibenate007.pkg.MySessionFactory.getSession(MySessionFactory.java:10)
at com.hibenate007.pkg.ClientProgram.main(ClientProgram.java:12)


Not sure what to do.
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have modified my hibernate.cfg.xml file.But now getting Employee.hbm.xml not found error.Where should I put my Employee.hbm.xml file or it will be auto generated by Hibernate.I am using hibernate 4.2.2 final.
 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw in a few examples that hibernate mapper goes in the same directory as corresponding entity java file.

You must read carefully this.
It gives you example of correct up-to-date namespace and example of <mapping> element which specifies full name (packagePath+className) of xml mapper.

If you see this in tutorial then this is obselete tutorial probably.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic