2017-04-24 77 views
0

我正在Eclipse上編寫一個簡單的Hibernate程序。我按步驟做了所有步驟,但在編譯後顯示:引起:org.hibernate.boot.registry.classloading.spi.ClassLoadingException:無法加載類[Employee]

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庫。

這是我的項目結構: here

+0

*產生的原因:拋出java.lang.ClassNotFoundException * –

+0

能否請你告訴我你的Employee類和你是如何創建Hibernate的Session堅持Employee對象? –

回答

1

入住休眠配置文件此項。可能是你已經改變了軟件包名稱,並忘記在配置文件中更改引用。

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

還將映射資源的標記更改爲映射類,並查看它是否工作。

+0

我和OP有同樣的問題,只是忘了更新配置XML文件。 +1用於回答不明確的問題,並且沒有明確定義問題。 –

+0

@Manoj,他沒有使用任何帶註釋的類映射,他正在使用資源映射,你不能讓他改變他的映射 – 2018-01-21 09:20:02

1

使用資源映射

由於您使用的是測繪資源,問題是在你的emp.hbm.xml提到的類路徑,因爲你Employee.javahibernatetutorial1類路徑將是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中提及您的類路徑,並且您需要使用映射類,不需要映射reso urce

//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> 
相關問題