1. 程式人生 > >Eclipse 通過Hibernate連結資料表生成實體類

Eclipse 通過Hibernate連結資料表生成實體類

4.4.1 配置hibernate configurations(hibernate連結資料庫)

1.開啟控制元件視窗

點選 window - open view - other

hibernate資料夾下開啟hibernate configurations

 

4.4.1-1

2.配置 configuration

 找到專案的hibernate.cfg.xml,配置好路徑和賬號密碼

 

hibernate configurations介面下,右鍵點選Add Configuration如上圖 4.4.1-1

點選ProjectBrowse選擇專案,再點選Configuration file

下的Setup拿到選擇Use existing找到專案中的hibernate.cfg.xml ,如下圖:

 

視窗先不關閉,繼續點選Classpath選項卡,新增專案和新增連結資料庫的jar包,如下圖:

 

完成以上步驟後點擊ok,然後回到視窗左邊重新整理配置,看到Database裡有資料庫名和表代表連結成功,如下圖:

 

4.4.2 建立程式碼生成配置

找到專案中hibernate.reveng.xml 配置想要生成實體類的表名,如下圖:

 

回到Hibernate介面,進入建立程式碼的配置

 

1.選擇配置,2選擇輸出的目錄,3填寫生成實體的包名,4.選擇配置好的hibernate.reveng.xml, 5

儲存配置

 

切換到Exporters選項卡,勾選如下圖:

 

4.4.3 執行配置檔案生成實體類

所有配置完後,在上圖點選Run按鈕,等到執行完即生成實體類在相應的目錄和包下


另外,有可能出現以下錯誤:

1.   

org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V

解決辦法是:在圖2的Edit Configuration 介面classpath去掉專案

2.  javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  

Java.naming.factory.initial

解決辦法:

去掉圖1的hibernate.cfg.xml中,sessionFactory 配置了name屬性即可

3.在切換兩個專案後執行hibernate.reveng.xml,有可能不報錯,但又不生成實體類。

解決:hibernate configurations介面下重新新建Add configurations ,到了選取hibernate.cfg.xml,和hibernate.reveng.xml時提示選擇新增,就可以了