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

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

问题描述

我只是想听到Hibernate专家对基于Hibernate / JPA的项目的数据库模式生成最佳实践的看法。尤其是:


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

  2. 在整个项目中,架构是使用Hibernate生成的,在系统发布到生产之前禁用自动模式生成并手动创建数据库模式会更好吗?

    在系统投入生产之后,维护实体类和数据库模式的最佳做法是什么(例如添加/重命名/更新列,重命名表等)?



解决方案


  1. 建议手动生成模式,一个支持数据库模式修订的工具,比如伟大的 Liquibase 。从实体生成模式在理论上是伟大的,但在实践中很脆弱,从长远来看会导致很多问题(请相信我)。

  2. 在制作中,手动生成和检查模式总是最好的。 你对一个实体进行更新并创建一个匹配的更新脚本您的数据库模式以反映实体更改。您可以创建一个自定义解决方案(我已经写了几个)或者使用更流行的东西,比如liquibase(它甚至支持模式更改回滚)。如果您使用的是诸如maven或ant之类的构建工具,建议将db schema update util插入到构建过程中,以使新构建与模式保持同步。


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

  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?

  2. 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?

  3. 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. 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).

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

  3. 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架构生成最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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