问题描述
我正在Eclipse上编写一个简单的Hibernate程序。我通过步骤完成了所有步骤,但在编译后显示出来:
由org.hibernate.boot.registry.classloading引起。 spi.ClassLoadingException:无法加载类[Employee]
导致:java.lang.ClassNotFoundException:无法加载请求的类:Employee
我也添加了所有必需的jar库。
这是我的项目结构:
使用资源映射
在使用映射资源时,问题出现在你的 emp.hbm.xml
,就像你在包中包含 Employee.java
hibernatetutorial1
你的类路径将是 hibernatetutorial1.Employee
。所以你需要在你的 emp.hbm.xml
//emp.hbm.xml
< hibernate-mapping>
< class name =hibernatetutorial1.Employeetable =tablename>
.......
.......
< / hibernate-mapping>
并将此资源映射到 Hibernate.cfg.xml
// Hibernate.cfg.xml
< hibernate-configuration>
< session-factory>
......
......
......
<映射资源=emp.hbm.xml/>
< / session-factory>
< / hibernate-configuration>
使用注释类映射
使用带注释的类会更好,因为它们可以减轻您的负担,如果您使用带注释的类,那么您需要在 Hibernate.cfg.xml
中提及您的类路径,而您需要使用映射类,不需要映射资源
//使用带注释的类映射不需要emp.hbm.xml(资源映射)
//Hibernate.cfg.xml
< hibernate-configuration>
< session-factory>
......
......
......
< mapping class =hibernatetutorial1.Employee/>
< / session-factory>
< / hibernate-configuration>
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:
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>
这篇关于造成者:org.hibernate.boot.registry.classloading.spi.ClassLoadingException:无法加载类[Employee]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!