java 引起:org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [Employee]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43578991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [Employee]
提问by UI Developer Nepal
I am writing a simple Hibernate program on Eclipse. I did everything steps by steps but then after compiling its showing:
我正在 Eclipse 上编写一个简单的 Hibernate 程序。我一步一步地做了所有事情,但是在编译它的显示之后:
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.
我也添加了所有必需的 jar 库。
回答by
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
,因为你Employee.java
的包里面hibernatetutorial1
的类路径会hibernatetutorial1.Employee
。所以你需要在你的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.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
最好使用带注释的类,因为它们可以减轻你的负担,如果你使用带注释的类,那么你需要在里面提到你的类路径Hibernate.cfg.xml
,你需要使用映射类,不需要映射资源
//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>
回答by Manoj Majumdar
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.
还要把映射资源的标签改成映射类,看看是否有效。