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

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

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

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


当前回答

如果你不想在你的应用程序中使用字符串,并正在寻找预定义的常量,请查看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";

其他回答

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

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

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

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.

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

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

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

首先,hbm2ddl配置属性的可能值如下:

none - No action is performed. The schema will not be generated. create-only - The database schema will be generated. drop - The database schema will be dropped. create - The database schema will be dropped and created afterward. create-drop - The database schema will be dropped and created afterward. Upon closing the SessionFactory, the database schema will be dropped. validate - The database schema will be validated using the entity mappings. update - The database schema will be updated by comparing the existing database schema with the entity mappings.

hibernate.hbm2ddl。Auto ="update"很方便,但如果您计划添加函数或执行一些自定义脚本,则不太灵活。

所以,最灵活的方法是使用Flyway。

但是,即使使用Flyway,仍然可以使用hbm2ddl生成初始迁移脚本。

我觉得你应该集中精力

SchemaExport Class 

这个类使你的配置动态 所以它可以让你选择你最喜欢的套间…

结帐 [架构导出]