只要在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"
/>