【问题标题】:Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [Employee]引起:org.hibernate.boot.registry.classloading.spi.ClassLoadingException:无法加载类[Employee]
【发布时间】:2017-09-20 14:21:02
【问题描述】:

我正在 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

我也添加了所有必需的 jar 库。

这是我的项目结构:

【问题讨论】:

  • 原因:java.lang.ClassNotFoundException
  • 能否请您告诉我您的 Employee 类以及您是如何创建休眠会话来持久化 Employee 对象的?

标签: java hibernate classnotfound


【解决方案1】:

在休眠配置文件中检查此条目。可能是你更改了包名,忘记更改配置文件中的引用。

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

同样将映射资源的标签改为映射类,看看是否有效。

【讨论】:

  • 我遇到了与 OP 相同的问题,只是忘记更新配置 xml 文件。 +1 用于回答不清楚且未明确定义问题的问题。
  • @Manoj,他没有使用任何带注释的类映射,他正在使用资源映射,你不能让他改变他的映射
【解决方案2】:

使用资源映射

当您使用映射资源时,问题在于您的emp.hbm.xml 中提到的类路径,因为您在包hibernatetutorial1 中有Employee.java,您的类路径将是hibernatetutorial1.Employee。所以你需要在你的emp.hbm.xml中提到这一点

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

并将这个资源映射到Hibernate.cfg.xml

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

使用带注释的类映射

最好使用带注释的类,因为它们可以减轻您的负担,如果您使用带注释的类,则需要在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>

【讨论】:

  • 这对我帮助很大。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多