2

I am writing a simple Hibernate program on Eclipse. I did everything steps by steps but then after compiling its showing:

Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [Employee]

Caused by: java.lang.ClassNotFoundException: Could not load requested class : Employee

I added all the required jar library too.

This is my project structure: here

Gholamali Irani
  • 4,391
  • 6
  • 28
  • 59

2 Answers2

3

Check this entry in hibernate config file. May be you have changed the package name and forgot to change the reference in config file.

<mapping class="Package of employee class"/>

Also change the tag of mapping resource to mapping class, and see if it works.

Manoj Majumdar
  • 505
  • 1
  • 4
  • 23
  • I had the same issue as the OP and just forgot to update the configuration xml file. +1 for answering to a question which is not clear and doesn't explicitly define the problem. – cihan adil seven Oct 17 '17 at 13:51
  • @Manoj, he is not using any annotated class mapping, he is using resource mapping, you can't make him change his mapping –  Jan 21 '18 at 09:20
2

Using resource mapping

As you are using a mapping resource, the problem is with class path mentioned in your emp.hbm.xml, as you have Employee.java inside the package hibernatetutorial1 your class path will be hibernatetutorial1.Employee. So you need to mention this in your emp.hbm.xml

//emp.hbm.xml
<hibernate-mapping>  
  <class name="hibernatetutorial1.Employee" table="tablename">  
  .......
  .......
</hibernate-mapping> 

and map this resource inside Hibernate.cfg.xml

//Hibernate.cfg.xml
<hibernate-configuration>  
    <session-factory>  
    ......
    ......
    ......
    <mapping resource="emp.hbm.xml"/>  
    </session-factory>
</hibernate-configuration>

Using annotated class mapping

It's better using annotated classes as they decrease your burden, if you are using annotated class then you need to mention your classpath inside Hibernate.cfg.xml and you need to use mapping class, no need of mapping resource

//using annotated class mapping no need of emp.hbm.xml(resource mapping)
//Hibernate.cfg.xml
<hibernate-configuration>  
    <session-factory>  
        ......
        ......
        ......
    <mapping class="hibernatetutorial1.Employee"/>  
    </session-factory>
</hibernate-configuration>