问题
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