Mingyang.net:hibernate.hbm2ddl.auto配置詳解【轉】

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   io   檔案   

原文地址:http://www.cnblogs.com/feilong3540717/archive/2011/12/19/2293038.html

hibernate.cfg.xml 中hibernate.hbm2ddl.auto配置節點如下:

<properties>    <property name="hibernate.show_sql" value="true" />     <property name="hibernate.hbm2ddl.auto" value="create" /></properties>

 

Hibernate Reference Documentation 3.3.1解釋如下:

Automatically validate or export schema DDL to the database when the SessionFactory is created.With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.eg. validate | update | create | create-drop

 

其實這個hibernate.hbm2ddl.auto參數的作用主要用於:自動建立|更新|驗證資料庫表結構。如果不是此方面的需求建議set value="none"。
create:每次載入hibernate時都會刪除上一次的產生的表,然後根據你的model類再重新來產生新表,哪怕兩次沒有任何改變也要這樣執行,這就是導致資料庫表資料丟失的一個重要原因。
create-drop :每次載入hibernate時根據model類產生表,但是sessionFactory一關閉,表就自動刪除。
update:最常用的屬性,第一次載入hibernate時根據model類會自動建立起表的結構(前提是先建立好資料庫),以後載入hibernate時根據 model類自動更新表結構,即使表結構改變了但表中的行仍然存在不會刪除以前的行。要注意的是當部署到伺服器後,表結構是不會被馬上建立起來的,是要等 應用第一次運行起來後才會。
validate :每次載入hibernate時,驗證建立資料庫表結構,只會和資料庫中的表進行比較,不會建立新表,但是會插入新值。

再說點“廢話”:
當我們把hibernate.hbm2ddl.auto=create時hibernate先用hbm2ddl來產生資料庫schema。
當我們把hibernate.cfg.xml檔案中hbm2ddl屬性注釋掉,這樣我們就取消了在啟動時用hbm2ddl來產生資料庫schema。通常 只有在不斷重複進行單元測試的時候才需要開啟它,但再次運行hbm2ddl會把你儲存的一切都刪除掉(drop)---- create配置的含義是:“在建立SessionFactory的時候,從scema中drop掉所以的表,再重新建立它們”。
注意,很多Hibernate新手在這一步會失敗,我們不時看到關於Table not found錯誤資訊的提問。但是,只要你根據上面描述的步驟來執行,就不會有這個問題,因為hbm2ddl會在第一次啟動並執行時候建立資料庫schema, 後續的應用程式重啟後還能繼續使用這個schema。假若你修改了映射,或者修改了資料庫schema,你必須把hbm2ddl重新開啟一次。

***********************************************************

這兩天在整理Spring + JPA(Hibernate實現),從網上copy了一段Hibernate串連參數的配置。

<properties>    <property name="hibernate.show_sql" value="true" />     <property name="hibernate.hbm2ddl.auto" value="create" /></properties>

結果在測試時,老是探索資料庫表資料丟失。這個參數以前沒怎麼用,查了一圈其它的東東,最後才定位到這個上面。趕緊查了一下Hibernate的參數配置,解釋如下:

hibernate.hbm2ddl.auto Automatically validate or export schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. eg. validate | update | create | create-drop

其實這個參數的作用主要用於:自動建立|更新|驗證資料庫表結構。如果不是此方面的需求建議set value="none".

其它幾個參數的意思,我解釋一下:

validate 載入hibernate時,驗證建立資料庫表結構
create 每次載入hibernate,重新建立資料庫表結構,這就是導致資料庫表資料丟失的原因。
create-drop 載入hibernate時建立,退出是刪除表結構
update 載入hibernate自動更新資料庫結構

以上4個屬性對同一設定檔下所用有的映射表都起作用

 

總結:

1.請謹慎使用此參數,沒必要就不要隨便用。

2.如果探索資料庫表丟失,請檢查hibernate.hbm2ddl.auto的配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.