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

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

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

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


当前回答

Validate:验证模式,不对数据库进行任何更改。 更新:使用当前执行查询更新架构。 Create:每次创建新的模式,并销毁之前的数据。 create-drop:当应用程序停止或显式关闭SessionFactory时删除模式。

其他回答

首先,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生成初始迁移脚本。

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

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操作。

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

我会使用liquibase来更新您的数据库。Hibernate的模式更新特性实际上只适用于开发新特性的开发人员。在生产环境中,需要更谨慎地处理db升级。

我觉得你应该集中精力

SchemaExport Class 

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

结帐 [架构导出]