Hibernate/JPA DB Schema 生成最佳实践 [英] Hibernate/JPA DB Schema Generation Best Practices

查看:20
本文介绍了Hibernate/JPA DB Schema 生成最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想听听 Hibernate 专家对基于 Hibernate/JPA 的项目的 DB 模式生成最佳实践的看法.特别是:

I just wanted to hear the opinion of Hibernate experts about DB schema generation best practices for Hibernate/JPA based projects. Especially:

  1. 项目刚开始的时候用什么策略?是否建议在此阶段让 Hibernate 自动生成模式,还是从项目的最早阶段手动创建数据库表更好?

  1. What strategy to use when the project has just started? Is it recommended to let Hibernate automatically generate the schema in this phase or is it better to create the database tables manually from earliest phases of the project?

假设在整个项目中使用 Hibernate 生成模式,是否最好禁用自动模式生成并在系统发布到生产之前手动创建数据库模式?

Pretending that throughout the project the schema was being generated using Hibernate, is it better to disable automatic schema generation and manually create the database schema just before the system is released into production?

在系统发布到生产环境后,维护实体类和数据库架构的最佳实践是什么(例如添加/重命名/更新列、重命名表等)?

And after the system has been released into production, what is the best practice for maintaining the entity classes and the DB schema (e.g. adding/renaming/updating columns, renaming tables, etc.)?

推荐答案

  1. 始终建议手动生成模式,最好使用支持数据库模式修订的工具,例如出色的 Liquibase.从实体生成模式在理论上很好,但在实践中很脆弱,从长远来看会导致很多问题(相信我).

  1. It's always recommended to generate the schema manually, preferably by a tool supporting database schema revisions, such as the great Liquibase. Generating the schema from the entities is great in theory, but were fragile in practice and causes lots of problems in the long run(trust me on this).

在生产中,最好手动生成并检查架构.

In productions it's always best to have manually generated and review the schema.

您对实体进行更新并创建匹配的更新脚本(修订版)来更新您的数据库架构以反映实体更改.您可以创建自定义解决方案(我已经写了一些)或使用更流行的东西,如 liquibase(它甚至支持架构更改回滚).如果您使用的是 maven 或 ant 等构建工具 - 建议将 db schema update util 插入构建过程,以便新构建与架构保持同步.

You make an update to an entity and create a matching update script(revision) to update your database schema to reflect the entity change. You can create a custom solution(I've written a few) or use something more popular like liquibase(it even supports schema changes rollbacks). If you're using a build tool such as maven or ant - it's recommend to plug the db schema update util into the build process so that fresh builds stay in sync with the schema.

这篇关于Hibernate/JPA DB Schema 生成最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆