前天做項(xiàng)目的時(shí)候表結(jié)構(gòu)和數(shù)據(jù)總是在程序執(zhí)行的時(shí)候無端的修改,折騰了好長時(shí)間,查了很長時(shí)間hibernate的數(shù)據(jù)庫映射文件和接口程序,始終沒有發(fā)現(xiàn)有什么錯誤,到最后才發(fā)現(xiàn)了它!
<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
其實(shí)這個參數(shù)的作用主要用于:自動創(chuàng)建|更新|驗(yàn)證數(shù)據(jù)庫表結(jié)構(gòu)。
如果沒有此方面的需求建議set value="none".
其它幾個參數(shù)的意思:
validate 加載hibernate時(shí),驗(yàn)證創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu)
create 每次加載hibernate,重新創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu)
create-drop 加載hibernate時(shí)創(chuàng)建,退出是刪除表結(jié)構(gòu)
update 加載hibernate自動更新數(shù)據(jù)庫結(jié)構(gòu)