hibernate 自動產生資料庫表

來源:互聯網
上載者:User

只要在hibernate.cfg.xml添加這句話,就可以自動產生資料表
<property name="hibernate.hbm2ddl.auto">update</property>


update:表示自動根據model對象來更新表結構,啟動hibernate時會自動檢查資料庫,如果缺少表,則自動建表;如果表裡缺少列,則自動添加列。

還有其他的參數:
create:啟動hibernate時,自動刪除原來的表,建立所有的表,所以每次啟動後的以前資料都會丟失。

create-drop:啟動hibernate時,自動建立表,程式關閉時,自動把相應的表都刪除。所以程式結束時,表和資料也不會再存在。

PS:資料庫要預先建立好,因為hibernate只會建表,不會建庫

 

==========================================

表結構和資料總是在程式執行的時候無端的修改,折騰了好長時間,查了很長時間hibernate的資料庫對應檔和介面程式,始終沒有發現有什麼錯誤,到最後才發現了它!
           <property name="hibernate.hbm2ddl.auto" value="update" />
解釋如下:

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自動更新資料庫結構

如果探索資料庫表丟失或新增,請檢查hibernate.hbm2ddl.auto的配置 可設定 <property
name="hibernate.hbm2ddl.auto" value="none"
/>

只要在hibernate.cfg.xml添加這句話,就可以自動產生資料表
<property name="hibernate.hbm2ddl.auto">update</property>


update:表示自動根據model對象來更新表結構,啟動hibernate時會自動檢查資料庫,如果缺少表,則自動建表;如果表裡缺少列,則自動添加列。

還有其他的參數:
create:啟動hibernate時,自動刪除原來的表,建立所有的表,所以每次啟動後的以前資料都會丟失。

create-drop:啟動hibernate時,自動建立表,程式關閉時,自動把相應的表都刪除。所以程式結束時,表和資料也不會再存在。

PS:資料庫要預先建立好,因為hibernate只會建表,不會建庫

 

==========================================

表結構和資料總是在程式執行的時候無端的修改,折騰了好長時間,查了很長時間hibernate的資料庫對應檔和介面程式,始終沒有發現有什麼錯誤,到最後才發現了它!
           <property name="hibernate.hbm2ddl.auto" value="update" />
解釋如下:

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自動更新資料庫結構

如果探索資料庫表丟失或新增,請檢查hibernate.hbm2ddl.auto的配置 可設定 <property
name="hibernate.hbm2ddl.auto" value="none"
/>

聯繫我們

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