JPA/Hibernate 支持迁移吗?

发布于 2024-09-27 10:21:29 字数 191 浏览 5 评论 0原文

我目前正在开发一个桌面应用程序,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-10-04 10:21:29

我通常让 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.

掐死时间 2024-10-04 10:21:29

也许我必须引入新实体、删除它们或者只是更改实体中的属性类型。

我没有任何经验,但 Liquibase 提供了一些 Hibernate 集成 并且可以比较您的映射针对数据库并生成适当的更改日志:

LiquiBase-Hibernate 集成将当前 Hibernate 映射所需的数据库更改记录到更改日志文件,然后您可以在执行之前根据需要检查和修改该文件。

仍在寻找机会使用它并找到我悬而未决的问题的答案:

  • 使用注释时它是否有效?
  • 它是否需要一个 hibernate.cfg.xml 文件(尽管这不会是一个大障碍)?

更新:好的,Nathan Voxland 在 此响应,答案是:

  • 它在使用注释时有效
  • 它需要 hibernate.cfg.xml(目前)

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 Integration and can compare your mappings against a database and generate the appropriate change log:

The LiquiBase-Hibernate integration records the database changes required by your current Hibernate mapping to a change log file which you can then inspect and modify as needed before executing.

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)?

Update: Ok, both questions are covered by Nathan Voxland in this response and the answers are:

  • yes it works when using annotations
  • yes it requires an hibernate.cfg.xml (for now)
热血少△年 2024-10-04 10:21:29

有两个选项:

  • db-to-hibernate - 手动将数据库更改镜像到实体。这意味着您的数据库“领先”
  • hibernate-to-db - 要么使用 hibernate.hbm2ddl.auto=update ,或者在更改实体后手动更改数据库 - 这里您的对象模型是“领先”

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文