如何使用 Hibernate 更新数据库架构而不丢失数据?

发布于 2024-07-16 07:08:48 字数 303 浏览 4 评论 0原文

假设您正在使用 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 技术交流群。

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

发布评论

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

评论(7

与风相奔跑 2024-07-23 07:08:49

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.

子栖 2024-07-23 07:08:49

我个人会跟踪迁移 SQL 脚本中的所有更改。

I personally keep track of all changes in a migration SQL script.

是你 2024-07-23 07:08:49

您可以使用 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.

云淡风轻 2024-07-23 07:08:49

对于一个应用程序,我使用 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.

棒棒糖 2024-07-23 07:08:49

作为对 Nathan Voxland 关于 LiquiBase 所说的话的进一步回应,这里有一个在 Windows 下执行 mySql 数据库迁移的示例:

例如将 mysql connector 放在 liquibase 发行版的 lib 文件夹下。

在 liquibase 发行版的根目录中创建文件属性 liquibase.properties 并插入此重复行:

driver: com.mysql.jdbc.Driver
classpath: lib\\mysql-connector-java-5.1.30.jar
url: jdbc:mysql://localhost:3306/OLDdatabase
username: root
password: pwd

以其他名称(例如 NEWdatabase)生成或检索更新的数据库。

现在,您将使用以下命令行提取文件 Migration.xml 中的差异:

liquibase diffChangeLog --referenceUrl="jdbc:mysql://localhost:3306/NEWdatabase" 
--referenceUsername=root --referencePassword=pwd > C:\Users\ME\Desktop\Migration.xml

最后使用刚刚生成的 Migration.xml 文件执行更新:

java -jar liquibase.jar --changeLogFile="C:\Users\ME\Desktop\Migration.xml" update

注意:所有此命令应从存在 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 :

driver: com.mysql.jdbc.Driver
classpath: lib\\mysql-connector-java-5.1.30.jar
url: jdbc:mysql://localhost:3306/OLDdatabase
username: root
password: pwd

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 :

liquibase diffChangeLog --referenceUrl="jdbc:mysql://localhost:3306/NEWdatabase" 
--referenceUsername=root --referencePassword=pwd > C:\Users\ME\Desktop\Migration.xml

Finally execute the update by using the just generated Migration.xml file :

java -jar liquibase.jar --changeLogFile="C:\Users\ME\Desktop\Migration.xml" update

NB: All this command lines should be executed from the liquibase home directory where liquibase.bat/.sh and liquibase.jar are present.

农村范ル 2024-07-23 07:08:49

我使用 hbm2ddl ant 任务来生成 ddl。 有一个选项可以在数据库中执行更改表/列。

请参阅 hbm2ddl ant 任务的“update”属性:

http://www.hibernate.org/hib_docs/tools/reference/en/html/ant.html#d0e1137

更新(默认:false):尝试并创建
更新脚本代表
之间的“增量”
数据库以及映射是什么
指定。 忽略创建/更新
属性。 (请勿使用
生产数据库,不保证
所有正确的增量都可以是
生成的也不是底层的
数据库实际上可以执行
所需的操作)

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

update(default: false): Try and create
an update script representing the
"delta" between what is in the
database and what the mappings
specify. Ignores create/update
attributes. (Do not use against
production databases, no guarantees at
all that the proper delta can be
generated nor that the underlying
database can actually execute the
needed operations)

浮萍、无处依 2024-07-23 07:08:49

您还可以使用DBMigrate。 它类似于 Liquibase :

类似于 Ruby 上的“rake migrate”
Rails 这个库可以让你管理
Java 的数据库升级
应用程序。

You can also use DBMigrate. It's similar to Liquibase :

Similar to 'rake migrate' for Ruby on
Rails this library lets you manage
database upgrades for your Java
applications.

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