我真的想知道更多关于更新,导出和值,可以给hibernate.hbm2ddl.auto 我需要知道什么时候使用更新,什么时候不?另一种选择是什么?

以下是在DB中可能发生的变化:

新表 旧表中的新列 列删除 事件解释列的数据类型发生变化 列的类型更改了其属性 表了 事件解释列值发生变化

在每种情况下,最佳解决方案是什么?


当前回答

hibernate.hbm2ddl。auto在创建sessionFactory时自动验证DDL并将其导出到模式。

默认情况下,不会自动对DB进行任何创建或修改。如果用户设置了以下值之一,那么它将自动进行DDL模式更改。

创建—正在创建模式 <输入键= " hibernate.hbm2ddl。汽车“价值= "创建" > 更新——更新现有的模式 <输入键= " hibernate.hbm2ddl。汽车“价值= "更新" > 验证—验证现有的模式 <输入键= " hibernate.hbm2ddl。汽车“价值= "验证" > 创建-删除—在会话开始和结束时自动创建和删除模式 <输入键= " hibernate.hbm2ddl。汽车“价值= " create-drop " >

其他回答

还有一个值none可以完全禁用它。

来自社区文档:

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.hbm2ddl。auto在创建sessionFactory时自动验证DDL并将其导出到模式。

默认情况下,不会自动对DB进行任何创建或修改。如果用户设置了以下值之一,那么它将自动进行DDL模式更改。

创建—正在创建模式 <输入键= " hibernate.hbm2ddl。汽车“价值= "创建" > 更新——更新现有的模式 <输入键= " hibernate.hbm2ddl。汽车“价值= "更新" > 验证—验证现有的模式 <输入键= " hibernate.hbm2ddl。汽车“价值= "验证" > 创建-删除—在会话开始和结束时自动创建和删除模式 <输入键= " hibernate.hbm2ddl。汽车“价值= " create-drop " >

虽然这是一个相当老的帖子,但我做了一些关于这个话题的研究,所以我想分享它。

auto

根据文档,它可以有四个有效值:

创建|更新|验证| Create -drop

以下是对这些值所显示的行为的解释:

创建:—创建模式,之前在模式中出现的数据(如果有)将丢失 更新:用给定的值更新模式。 Validate:—验证模式。它对DB没有任何改变。 create-drop:创建模式时销毁之前存在的数据(如果有的话)。它还在SessionFactory关闭时删除数据库模式。

以下是值得注意的要点:

In case of update, if schema is not present in the DB then the schema is created. In case of validate, if schema does not exists in DB, it is not created. Instead, it will throw an error:- Table not found:<table name> In case of create-drop, schema is not dropped on closing the session. It drops only on closing the SessionFactory. In case if i give any value to this property(say abc, instead of above four values discussed above) or it is just left blank. It shows following behaviour: -If schema is not present in the DB:- It creates the schema -If schema is present in the DB:- update the schema.

综上所述…… 注意,这个属性叫做dll。auto和应该只控制dll操作(创建/删除模式/表),我惊讶地发现它也与dml有关:只有更新将允许插入数据,这是dml操作。

在试图将数据填充到内存数据库时被此捕获;只有更新有效。