eclipse利用Hibernate Tools產生Hibernate Mapping檔案及PO類

來源:互聯網
上載者:User

俗話說得不錯,“變化總比計劃快”。在項目,在我們這些程式員當中也是常常會遇到的問題。因為就算需求調研得再詳細,往往一定還會修改的。為了適應變化,構造易維護易擴充的項目往往是我們的目標之一。

 

在使用資料庫時,相信很多程式員會有我一樣的煩惱。就是資料庫發生變化時,項目中的hibernate配置的連鎖反應,mapping映射需要修改,PO類需要更改等。使用JBossTools中的Hibernate Tools,將為我們解決這一大難題。 一、開發環境

JBoss Tools下載

http://www.jboss.org/tools/download.html

 

在這裡,我是選擇線上安裝的方式。並且eclipse版本為3.6.2,所以我的選擇是:http://www.jboss.org/tools/download/installation/update_3_2.html

 

(題外:安裝完之後,你會發現eclipse的效能大增。除了html、JSP等編輯器外,還有諸多J2EE外掛程式)

  二、配置hibernate configurations 1.開啟控制項視窗

點擊 window - open view - other

 

在hibernate檔案夾下開啟hibernate configurations 2.add configuration

設定資料庫串連資源

 


圖中的MySQL是我之前所建立的資料庫連接,若之前沒配置的話可以點擊New,然後設定資料庫串連。

 

查看資料庫,測試是否正常。

 


三、Hibernate代碼產生器 1. 增加Hibernate Code Generation按鈕

點擊 window - customize perspective


2.點擊Hibernate Code Generation configuration


  3.建立,管理代碼組建組態


  console configuration:選擇在第二點中我們建立的資料庫連接。 output directory:配置執行後輸出的目錄,產生hbm、PO類等。 package:包名,這裡請輸入PO類存放的路徑。 reveng.xml:產生mapping和PO類的重要設定檔。 reveng.xml若沒產生可以在這裡建立。
3.hibernate.reveng.xml

reveng.xml檔案,讓我們可以控制外掛程式產生後的hbm和PO類的各種屬性。

 

以下是參考:

  Xml代碼   <?xml version="1.0" encoding="UTF-8"?>   <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >      <hibernate-reverse-engineering>       <type-mapping>           <sql-type jdbc-type="VARCHAR" length="1" hibernate-type="yes_no" />           <sql-type jdbc-type="VARCHAR" length="21" not-null="false"               hibernate-type="cn.com.timekey.drugmonitor.po.type.RoleEnumType" />           <sql-type jdbc-type="VARCHAR" length="15" not-null="false"               hibernate-type="cn.com.timekey.drugmonitor.po.type.PrescriptionTypeEnumType" />       </type-mapping>       <!-- BIN$ is recycle bin tables in Oracle -->       <table-filter match-name="BIN$.*" exclude="true" />              <table-filter match-catalog="cmm" match-name=".*" />          <!-- table allows you to override/define how reverse engineering is done            for a specific table -->       <table name="user" catalog="cmm">           <primary-key>               <generator class="uuid.hex" />               <key-column name="ID" />           </primary-key>       </table>   </hibernate-reverse-engineering>    

 

 

1.通過type-mapping代碼塊,我們可以自由的映射出資料庫欄位和hibernate中的對應欄位。

 

  Xml代碼   <sql-type jdbc-type="VARCHAR" length="1" hibernate-type="yes_no" />    

意思是:把資料庫中VARCHAR類型並且長度為1的欄位,映射成PO類的boolean屬性。

 

  Xml代碼   <sql-type jdbc-type="VARCHAR" length="21" not-null="false"               hibernate-type="cn.com.timekey.drugmonitor.po.type.RoleEnumType" />  

 

同理,這裡的意思是:把資料庫中VARCHAR類型,長度為21的欄位對應成我自訂的hibernate類型。

 

2.通過table-filter代碼塊,我們可以過濾及指定指令碼的目標table。

 

  Xml代碼   <table-filter match-catalog="cmm" match-name=".*" />    

意思是:cmm資料庫下的所有表。

 

有些情況下,我們指令碼執行的並非全部表,這裡恰好可以輕鬆幫我們達到這個目的。

 

3.table代碼塊,提供給我們用來覆蓋產生的mapping配置。

  Xml代碼   <table name="user" catalog="cmm">       <primary-key>           <generator class="uuid.hex" />           <key-column name="ID" />       </primary-key>   </table>    

 

這裡相信大家應該比較容易猜到了,是起到指定user表中的主鍵建置規則為uuid.hex。

 

4.自訂外鍵配置

 

  Xml代碼   <foreign-key constraint-name="prescription_drug_ibfk_3">       <column-ref local-column="DRUG_ID" />       <many-to-one property="drug" cascade="save-update" />   </foreign-key>  

 

 

同樣在<table>代碼塊內。

constraint-name:約束名,要對應外鍵的約束名字。

column-ref:配置外鍵欄位名。

many-to-one:配置外鍵參數,包括屬性名稱等,例中是添加cascade的層級為save-update。預設為空白,即none。

 

 

更多的reveng.xml說明可參考:http://docs.jboss.org/tools/3.2.0.GA/en/hibernatetools/html_single/index.html#hibernaterevengxmlfile

  4.選擇匯出的檔案


在這裡我是選擇產生mapping的 hbm對應檔及PO類。DAO類一般會用自己的,並且很多時候都是與spring整合的環境下,所以作用不大。cfg.xml檔案其實也有點用,匯入hbm.xml對應檔的時候,可以參考此檔案來拷貝了。

  5.回合組態檔案

這一切都OK之後,按下RUN鍵,即可產生我們想要的mapping對應檔及PO類了。

 

  後記

產生的PO類並沒有產生序號是我比較糾結的,但從開發人員的角度來看,這個也是可以理解。


http://kennylee26.iteye.com/blog/1039542

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.