如何使用 Hibernate 更新数据库架构而不丢失数据?
假设您正在使用 Hibernate 和 JBoss 开发 Java EE 应用程序。 您有一台正在运行的服务器,其中包含一些重要数据。 您偶尔(1-2 周)发布应用程序的下一个版本,它们在持久层中进行了一系列更改:
- 新实体
- 删除的实体
- 属性类型更改
- 属性名称更改
- 关系更改
如何有效地设置系统更新数据库模式并保留数据? 据我所知(我可能是错误的),Hibernate 不执行更改列、删除/更改约束。
谢谢你, 阿尔乔姆 B.
Imagine you are developing a Java EE app using Hibernate and JBoss. You have a running server that has some important data on it. You release the next version of the app once in a while (1-2 weeks) and they have a bunch of changes in the persistence layer:
- New entities
- Removed entities
- Attribute type changes
- Attribute name changes
- Relationship changes
How do you effectively set up a system that updates the database schema and preserves the data? As far as I know (I may be mistaking), Hibernate doesn't perform alter column, drop/alter constraint.
Thank you,
Artem B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
LiquiBase 是您最好的选择。 它有一个 hibernate 集成 模式,使用 Hibernate 的 hbm2ddl 来比较您的数据库和 hibernate 映射,但是它不是自动更新数据库,而是输出一个 liquibase 更改日志文件,可以在实际运行之前检查该文件。
虽然更方便,但任何比较数据库和休眠映射的工具都会出错。 请参阅http://www.liquibase.org/2007 /06/the-problem-with-database-diffs.html 例如。 使用 liquibase,您可以在开发时建立数据库更改列表,该格式可以在分支和合并的代码中幸存下来。
LiquiBase is your best bet. It has a hibernate integration mode that uses Hibernate's hbm2ddl to compare your database and your hibernate mapping, but rather than updating the database automatically, it outputs a liquibase changelog file which can be inspected before actually running.
While more convenient, any tool that does a comparison of your database and your hibernate mappings is going to make mistakes. See http://www.liquibase.org/2007/06/the-problem-with-database-diffs.html for examples. With liquibase you build up a list of database changes as you develop in a format that can survive code with branches and merges.
我个人会跟踪迁移 SQL 脚本中的所有更改。
I personally keep track of all changes in a migration SQL script.
您可以使用 https://github.com/Devskiller/jpa2ddl 工具,该工具提供 Maven 和 Gradle 插件能够基于 JPA 实体为 Flyway 生成自动架构迁移。 它还包括所有属性、方言、用户类型、命名策略等。
You can use https://github.com/Devskiller/jpa2ddl tool which provides Maven and Gradle plugin and is capable of generating automated schema migrations for Flyway based on JPA entities. It also includes all properties, dialects, user-types, naming strategies, etc.
对于一个应用程序,我使用 SchemaUpdate,它直接从引导类内置到 Hibernate 中,因此每次应用程序启动时都会检查架构。 这负责添加新的列或表,这主要发生在成熟的应用程序中。 为了处理特殊情况,例如删除列,引导程序只需在 try/catch 中手动运行 ddl,因此如果它已经被删除过一次,它只会默默地抛出错误。 我不确定我是否会在生产应用程序中使用关键任务数据来执行此操作,但在几年和数百次部署中,我从未遇到过问题。
For one app I use SchemaUpdate, which is built in to Hibernate, straight from a bootstrap class so the schema is checked every time the app starts up. That takes care of adding new columns or tables which is mostly what happens to a mature app. To handle special cases, like dropping columns, the bootstrap just manually runs the ddl in a try/catch so if it's already been dropped once, it just silently throws an error. I'm not sure I'd do this with mission critical data in a production app, but in several years and hundreds of deployments, I've never had a problem with it.
作为对 Nathan Voxland 关于 LiquiBase 所说的话的进一步回应,这里有一个在 Windows 下执行 mySql 数据库迁移的示例:
例如将 mysql connector 放在 liquibase 发行版的 lib 文件夹下。
在 liquibase 发行版的根目录中创建文件属性 liquibase.properties 并插入此重复行:
以其他名称(例如 NEWdatabase)生成或检索更新的数据库。
现在,您将使用以下命令行提取文件 Migration.xml 中的差异:
最后使用刚刚生成的 Migration.xml 文件执行更新:
注意:所有此命令应从存在 liquibase.bat/.sh 和 liquibase.jar 的 liquibase 主目录执行这些行。
As a further response of what Nathan Voxland said about LiquiBase, here's an example to execute the migration under Windows for a mySql database:
Put the the mysql connector under lib folder in liquibase distribution for example.
Create a file properties liquibase.properties in the root of the liquibase distribution and insert this recurrent lines :
Generate or retrieve an updated database under another name for example NEWdatabase.
Now you will exctract differences in a file Migration.xml with the following command line :
Finally execute the update by using the just generated Migration.xml file :
NB: All this command lines should be executed from the liquibase home directory where liquibase.bat/.sh and liquibase.jar are present.
我使用 hbm2ddl ant 任务来生成 ddl。 有一个选项可以在数据库中执行更改表/列。
请参阅 hbm2ddl ant 任务的“update”属性:
http://www.hibernate.org/hib_docs/tools/reference/en/html/ant.html#d0e1137
I use the hbm2ddl ant task to generate my ddl. There is an option that will perform alter tables/columns in your database.
Please see the "update" attribute of the hbm2ddl ant task:
http://www.hibernate.org/hib_docs/tools/reference/en/html/ant.html#d0e1137
您还可以使用DBMigrate。 它类似于 Liquibase :
You can also use DBMigrate. It's similar to Liquibase :