如何使用 Hibernate 创建数据库架构

发布于 2024-07-18 18:51:32 字数 578 浏览 4 评论 0原文

阅读Hibernate:生产中的hbm2ddl.auto=update?后出现了一些问题。 首先,我使用 Hibernate 的原因是独立于数据库供应商(不需要编写 10 个版本的“相同”sql 查询,例如 tsql 与 sql)。

当创建数据库模式(生产环境)时,我的问题出现了。 据我所知,我有两种选择。

  1. hbm2dll = 更新
  2. 纯 sql (ddl) 脚本。

第一种替代方案在上面的线程中得到了广泛讨论。 第二种选择很糟糕,因为这意味着我又回到了第一个问题:“不想创建依赖于数据库供应商的 sql 语句”。 (如果“所有”(至少是 Hibernate 支持的数据库)正在实现 DDL< /a> (用于定义和检查数据库结构的 SQL 子集。)等于)。

After reading Hibernate: hbm2ddl.auto=update in production? some questions arose.
First of all, the reason for I'm using Hibernate is to be database vendor independent (no need to write 10 versions of the "same" sql query, eg. tsql vs. sql).

My problem appears when it's time to create the database schemas (production environment). As far as I can see I have two alternatives.

  1. hbm2dll = update
  2. pure sql (ddl) scripts.

The first alternative is widely discussed in the thread above.
The second alternative is bad because that means I'm back to my first problem: "Don't want to create sql statements that are database vendor dependent". (This statement could be false if "all" (atlast the databases Hibernate support) are implementing the DDL (The subset of SQL used for defining and examining the structure of a database.) equal).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

莫相离 2024-07-25 18:51:32

在开发/暂存模式中进行所有更改,并在生产中手动(或自动,但不要让 hibernate 运行它们)传输和执行脚本。 由于 hbm2ddl 更新并未涵盖所有情况,因此脚本可能需要一些调整。

事实上,我从来没有让 hibernate 对任何数据库运行 ddl。 我使用 hbm2ddl 生成文本:

org.hibernate.tool.hbm2ddl.SchemaExport --config=hibernate.cfg.xml --text --format --delimiter=;

org.hibernate.tool.hbm2ddl.SchemaUpdate --config=hibernate.cfg.xml --text --format --delimiter=;

Do all changes in development/staging mode and transfer and execute scripts in production manually (or automatically, but don't let hibernate run them). The scripts may need some tunning since hbm2ddl update does not cover all cases.

In fact I never let hibernate run ddl against any database. I use hbm2ddl to generate text:

org.hibernate.tool.hbm2ddl.SchemaExport --config=hibernate.cfg.xml --text --format --delimiter=;

org.hibernate.tool.hbm2ddl.SchemaUpdate --config=hibernate.cfg.xml --text --format --delimiter=;
﹂绝世的画 2024-07-25 18:51:32

通常转储 JPA 架构的工具基于 SchemaExport 工具,该工具仅读取静态元数据。

有一个 Maven/Gradle 插件 https://github.com/Devskiller/jpa2ddl 可以生成 JPA架构。 包括所有属性、命名策略、用户类型等。

您还可以使用它为 Flyway 生成自动架构迁移。

Normally tools that dumps the JPA schema are based on SchemaExport tool, which reads only the static metadata.

There is a Maven/Gradle plugin https://github.com/Devskiller/jpa2ddl which generates the JPA schema. In includes all properties, namings strategies, user types, etc.

You can also use it to generate automated schema migrations for Flyway.

故事灯 2024-07-25 18:51:32

我喜欢 Hibernate(非常喜欢),并且我认为它可以编写出一些质量非常好的代码,但是我一会把它放到生产数据库上,就会让一只非常有礼貌的灰熊来照看。 一切都可能会在一段时间内进展顺利,但一旦发生变坏的事件,那就是真的很糟糕。

我的建议是在测试环境中,让 hibernate 生成数据库模式。 在测试环境中测试这些内容。 然后将这些脚本带到生产环境并运行它们。 注意那里的特殊性; 即使测试非常成功,我仍然不会允许 Hibernate 在生产服务器上疯狂。 获取 Hibernate schemagen 的输出,对其进行测试,经过验证后,将其部署到生产服务器。

I like Hibernate (a LOT), and I think it makes some incredibly good quality code, but I would no sooner turn it loose on a production database than I'd let a very well-mannered grizzly bear babysit. Everything can go great for a while, but the one incident where it goes bad is REALLY bad.

My suggestion would be in a test environment, have hibernate generate database schemas. Test those in a test environment. Then take those scripts to the production environment and run them. Note the specificity there; even if the tests succeed fantastically, I STILL wouldn't just allow Hibernate to go wild on the production server. Take the output of Hibernate schemagen, test it, and once it's validated, then deploy that to the production server.

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