JPA/Hibernate 支持迁移吗?
我目前正在开发一个桌面应用程序,使用 JPA/Hibernate 将数据保存在 H2 数据库中。我很好奇如果我将来由于某种原因需要更改数据库架构,我的选择是什么。也许我必须引入新实体、删除它们或者只是更改实体中的属性类型。
- JPA/Hibernate 是否支持执行此操作?
- 我是否必须手动编写解决方案脚本?
I'm currently working on a desktop application using JPA/Hibernate to persist data in a H2 database. I'm curious what my options are if I need to make changes to the database schema in the future for some reason. Maybe I'll have to introduce new entities, remove them or just change the types of properties in an entity.
- Is there support in JPA/Hibernate to do this?
- Would I have to manually script a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常让 Hibernate 在开发过程中生成 DDL,然后在部署到测试服务器时创建手动 SQL 迁移脚本(后来我也将其用于 UAT 和实时服务器)。
Hibernate中的DDL生成根本不提供对数据迁移的支持,如果你只做添加非空字段的工作,DDL生成无法帮助你。
我还没有找到任何真正有用的迁移抽象来帮助解决这个问题。
有很多库(请查看这个SO问题作为示例),但是当您'在执行诸如使用联合继承将现有实体拆分为层次结构之类的操作时,您总是会回到纯 SQL。
I usually let Hibernate generate the DDL during development and then create a manual SQL migration script when deploying to the test server (which I later use for UAT and live servers as well).
The DDL generation in Hibernate does not offer support for data migration at all, if you only do as much as adding a non-null field, DDL generation cannot help you.
I have yet to find any truely useful migration abstraction to help with this.
There are a number of libraries (have a look at this SO question for examples), but when you're doing something like splitting an existing entity into a hierarchy using joined inheritance, you're always back to plain SQL.
我没有任何经验,但 Liquibase 提供了一些 Hibernate 集成 并且可以比较您的映射针对数据库并生成适当的更改日志:
仍在寻找机会使用它并找到我悬而未决的问题的答案:
更新:好的,Nathan Voxland 在 此响应,答案是:
I don't have any experience with it but Liquibase provides some Hibernate Integration and can compare your mappings against a database and generate the appropriate change log:
Still looking for an opportunity to play with it and find some answers to my pending questions:
hibernate.cfg.xml
file (although this wouldn't be a big impediment)?Update: Ok, both questions are covered by Nathan Voxland in this response and the answers are:
有两个选项:
There are two options:
hibernate.hbm2ddl.auto=update
, or manually change the DB after changing your entity - here your object model is "leading"