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

落花浮王杯 提交于 2019-12-20 05:46:29

问题


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:


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/43578991/caused-by-org-hibernate-boot-registry-classloading-spi-classloadingexception-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!