我真的想知道更多关于更新,导出和值,可以给hibernate.hbm2ddl.auto 我需要知道什么时候使用更新,什么时候不?另一种选择是什么?
以下是在DB中可能发生的变化:
新表 旧表中的新列 列删除 事件解释列的数据类型发生变化 列的类型更改了其属性 表了 事件解释列值发生变化
在每种情况下,最佳解决方案是什么?
我真的想知道更多关于更新,导出和值,可以给hibernate.hbm2ddl.auto 我需要知道什么时候使用更新,什么时候不?另一种选择是什么?
以下是在DB中可能发生的变化:
新表 旧表中的新列 列删除 事件解释列的数据类型发生变化 列的类型更改了其属性 表了 事件解释列值发生变化
在每种情况下,最佳解决方案是什么?
当前回答
validate: It validates the schema and makes no changes to the DB. Assume you have added a new column in the mapping file and perform the insert operation, it will throw an Exception "missing the XYZ column" because the existing schema is different than the object you are going to insert. If you alter the table by adding that new column manually then perform the Insert operation then it will definitely insert all columns along with the new column to the Table. Means it doesn't make any changes/alters the existing schema/table.
更新:在执行操作时,会改变数据库中已有的表。 您可以使用hbm2ddl的这个选项添加或删除列。 但是如果你要添加一个'NOT NULL'的新列,那么它将忽略向DB添加特定的列。因为如果你想在现有的表中添加“NOT NULL”列,表必须是空的。
其他回答
我会使用liquibase来更新您的数据库。Hibernate的模式更新特性实际上只适用于开发新特性的开发人员。在生产环境中,需要更谨慎地处理db升级。
validate: It validates the schema and makes no changes to the DB. Assume you have added a new column in the mapping file and perform the insert operation, it will throw an Exception "missing the XYZ column" because the existing schema is different than the object you are going to insert. If you alter the table by adding that new column manually then perform the Insert operation then it will definitely insert all columns along with the new column to the Table. Means it doesn't make any changes/alters the existing schema/table.
更新:在执行操作时,会改变数据库中已有的表。 您可以使用hbm2ddl的这个选项添加或删除列。 但是如果你要添加一个'NOT NULL'的新列,那么它将忽略向DB添加特定的列。因为如果你想在现有的表中添加“NOT NULL”列,表必须是空的。
hibernate.hbm2ddl。auto在创建sessionFactory时自动验证DDL并将其导出到模式。
默认情况下,不会自动对DB进行任何创建或修改。如果用户设置了以下值之一,那么它将自动进行DDL模式更改。
创建—正在创建模式 <输入键= " hibernate.hbm2ddl。汽车“价值= "创建" > 更新——更新现有的模式 <输入键= " hibernate.hbm2ddl。汽车“价值= "更新" > 验证—验证现有的模式 <输入键= " hibernate.hbm2ddl。汽车“价值= "验证" > 创建-删除—在会话开始和结束时自动创建和删除模式 <输入键= " hibernate.hbm2ddl。汽车“价值= " create-drop " >
来自社区文档:
hibernate.hbm2ddl。auto在创建SessionFactory时自动验证或将模式DDL导出到数据库。使用create-drop,当SessionFactory显式关闭时,数据库模式将被删除。 例如:validate | update | create | create-drop
所以可能的选项是,
Validate:验证模式,不对数据库进行任何更改。 Create-only:创建数据库。 Drop:生成数据库删除。 Update:更新模式。 Create:创建模式,销毁之前的数据。 create-drop:当SessionFactory显式关闭时删除模式,通常是在应用程序停止时。 None:不对模式进行任何操作,不对数据库进行任何更改
这些选项似乎是开发人员的工具,而不是为了方便任何生产级别的数据库,你可能想看看下面的问题;Hibernate: hbm2ddl。自动=更新生产?
如果你不想在你的应用程序中使用字符串,并正在寻找预定义的常量,请查看Hibernate JAR中包含的org.hibernate.cfg.AvailableSettings类,在那里你可以找到一个用于所有可能设置的常量。以你为例:
/**
* Auto export/update schema using hbm2ddl tool. Valid values are <tt>update</tt>,
* <tt>create</tt>, <tt>create-drop</tt> and <tt>validate</tt>.
*/
String HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";