Java JPA/Hibernate 支持迁移吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3923574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA/Hibernate support for migration?
提问by willcodejavaforfood
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.
我目前正在开发一个使用 JPA/Hibernate 将数据保存在 H2 数据库中的桌面应用程序。如果将来出于某种原因需要更改数据库架构,我很好奇我的选择是什么。也许我将不得不引入新实体、删除它们或仅更改实体中的属性类型。
- Is there support in JPA/Hibernate to do this?
- Would I have to manually script a solution?
- JPA/Hibernate 是否支持这样做?
- 我是否必须手动编写解决方案的脚本?
采纳答案by Henning
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).
我通常在开发过程中让 Hibernate 生成 DDL,然后在部署到测试服务器时创建手动 SQL 迁移脚本(我后来也用于 UAT 和实时服务器)。
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.
Hibernate 中的 DDL 生成根本不支持数据迁移,如果您只是添加一个非空字段,那么 DDL 生成无法帮助您。
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 questionfor 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.
有许多库(请查看此 SO 问题的示例),但是当您执行诸如使用连接继承将现有实体拆分为层次结构之类的操作时,您总是会回到普通 SQL。
回答by Bozho
There are two options:
有两种选择:
- db-to-hibernate - mirror DB changes to your entities manually. This means your DB is "leading"
- hibernate-to-db - either use
hibernate.hbm2ddl.auto=update
, or manually change the DB after changing your entity - here your object model is "leading"
- db-to-hibernate - 手动将数据库更改镜像到您的实体。这意味着您的数据库是“领先的”
- hibernate-to-db -
hibernate.hbm2ddl.auto=update
在更改实体后使用或手动更改数据库 - 这里您的对象模型是“领先的”
回答by Pascal Thivent
Maybe I'll have to introduce new entities, remove them or just change the types of properties in an entity.
也许我将不得不引入新实体、删除它们或仅更改实体中的属性类型。
I don't have any experience with it but Liquibase provides some Hibernate Integrationand can compare your mappings against a database and generate the appropriate change log:
我没有任何经验,但 Liquibase 提供了一些Hibernate 集成,可以将您的映射与数据库进行比较并生成适当的更改日志:
The LiquiBase-Hibernate integration records the database changes required by your current Hibernate mapping to a change log filewhich you can then inspect and modify as needed before executing.
LiquiBase-Hibernate 集成将当前 Hibernate 映射所需的数据库更改记录到更改日志文件中,然后您可以在执行前根据需要检查和修改该文件。
Still looking for an opportunity to play with it and find some answers to my pending questions:
仍在寻找机会使用它并找到一些悬而未决的问题的答案:
- does it work when using annotations?
- does it require an
hibernate.cfg.xml
file (although this wouldn't be a big impediment)?
- 它在使用注释时有效吗?
- 它是否需要一个
hibernate.cfg.xml
文件(虽然这不会是一个大障碍)?
Update:Ok, both questions are covered by Nathan Voxland in this responseand the answers are:
更新:好的,Nathan Voxland 在此回复中涵盖了这两个问题,答案是:
- yesit works when using annotations
- yesit requires an hibernate.cfg.xml (for now)
- 是的,它在使用注释时有效
- 是的,它需要一个 hibernate.cfg.xml(目前)