使用 JPA 时的标准工作流程

发布于 2024-09-02 18:26:40 字数 373 浏览 3 评论 0 原文

我目前正在尝试与 JPA 合作。我情不自禁地觉得自己错过了一些东西或者以错误的方式做事。到目前为止,这似乎只是被迫的。

我想到目前为止我所知道的是,有几种使用 JPA 的方法和支持这一点的工具。

  • 您可以使用注释在 Java 中完成所有操作,并让 JPA(无论您决定使用什么实现)创建您的模式并在发生更改时更新它。
  • 您可以使用工具对数据库进行逆向工程并为您生成实体类。当架构更新时,您必须重新生成这些类,或手动更新它们。

似乎两者都有缺点,也都有优点(就像所有事情一样)。 我的问题是,在理想情况下,JPA 的标准工作流程是什么? 大多数模式在维护阶段,特别是在开发阶段都需要更新,那么如何处理呢?

I am currently trying to wrap my head around working with JPA. I can't help but feel like I am missing something or doing it the wrong way. It just seems forced so far.

What I think I know so far is that their are couple of ways to work with JPA and tools to support this.

  • You can do everything in Java using annotations, and let JPA (whatever implementation you decide to use) create your schema and update it when changes are made.
  • You can use a tool to reverse engineer you database and generate the entity classes for you. When the schema is updated you have to regenerate these classes, or manually update them.

There seems to be drawbacks to both, and benefits to both (as with all things). My question is in an ideal situation what is the standard workflow with JPA? Most schemas will require updates during the maintenance phase and especially during the development phase, so how is this handled?

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

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

发布评论

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

评论(3

橘虞初梦 2024-09-09 18:26:41

从带注释的实体生成数据库模式并不总是一个好方法。尽管理论上听起来不错,但实际上生成的模式通常不是最佳的,并且无法满足经验丰富的 DBA。

我在工作流程中遵循的方法是分别创建实体和数据库模式,同时仍然使用非常智能的工具来创建模式 - 类似于 Liquibase,与数据库无关,支持修订、回滚等...或者是一个自定义烘焙迁移工具,只需运行高度优化的数据库特定 SQL 脚本。

这对您来说可能听起来不太理想,但我可以保证它可以完成工作并保持与模式相关的代码一致,因为正如 grigory 指出的那样 - 无论如何,并不是与数据库相关的所有内容都可以从实体生成。

但是,我可以从正在运行单元和集成测试的测试数据库的实体生成模式。假设你使用的是 PostgreSQL 是生产环境,你可能会决定加快单元测试的速度,运行一些嵌入式内存数据库,比如 H2,它是在测试开始之前从实体创建的,并自动消失(因为它是在内存中的) )测试完成执行后。这是一种非常常见的做法。

It's not always a good approach to generate the DB schema from the annotated entities. Although in theory it sounds great - in practice often the generated schema is not optimal and would not satisfy and experienced DBA.

The approach that I follow in my workflow is to create the entities and db schema separately, while still using a pretty intelligent tool for the schema creating - either something like Liquibase, that is database agnostic, supports revisions, rollbacks, etc... or a custom baked migration tool that simply runs heavily optimized db specific sql scripts.

It probably sounds to you less than ideal, but I can assure it gets the jobs done and keep your schema related code consistent since, as grigory pointed out - not everything related to the database can be generated from the entities anyways.

I can, however, be useful to generate the schema from the entities for the test database against which unit and integration tests are being run. Assuming you're using say PostgreSQL is production you might decide to speed things up for the unit tests running some embedded in-memory database like H2 which gets created from the entities before the tests are started and disappears automatically(since it was in-memory) after the tests finish executing. This is a very common practice.

森末i 2024-09-09 18:26:41

像往常一样,答案是这取决于...

理想的方法(在理想的世界中)可能是您的第一个选择:使用 JPA 注释维护所有内容,并使用实用工具(例如使用 Hibernate Maven 插件)。

这取决于对数据库工件的支持级别 - 并非所有内容都属于或适合注释。这就是为什么我的项目通常对两者使用并行维护,并使用单元测试来保持它们同步。

它还取决于可用的资源。如果您有一位专门的 DBA 负责您的数据库,那么将维护工作委托给她是有意义的。

其他考虑因素是有多少数据库开发实际上是在 JPA 中完成的。是否还有使用相同后端的存储过程或其他非 JPA 应用程序,或者您可能只是与其他团队的数据库集成......

As usual the answer is it depends...

Ideal approach (in ideal world) would probably be your 1st option: maintain everything using JPA annotations and forward engineer database artifacts using utility tool (e.g. use Hibernate Maven plugin).

It depends on the level of support for your database artifacts - not everything either belongs or suitable for annotations. That is why my projects usually use parallel maintenance for both and using unit tests to keep them in sync.

It also depends on resources available. If you have a dedicated DBA who is responsible for your database then delegating maintenance to her would make sense.

Other consideration is how much database development is really done in JPA. Are there also stored procedures or other non-JPA applications that use the same back-end, or maybe you just integrate with other team's database...

凉城已无爱 2024-09-09 18:26:41

如果这是一个现有的应用程序,我会检查您现有的应用程序,如果数据库结构复杂(如 DDL 所示)并且 DDL 显示正在数据库本身上完成重要逻辑,那么您最好使用纯 SQL 和让 DBA 维护您的数据结构。当数据库结构已经很复杂并且此时使用 JPA 没有任何商业利益时,JPA 就不太适合了。

需要做的是一个迁移到 JPA 的项目。这样做有几个优点:

  • 业务逻辑从数据库层(很难水平扩展)移至应用程序层。
  • Java 开发人员通常比 DBA 便宜。尽管您仍然需要同时具备数据库思维和 Java 思维的人来正确地完成这项工作,但这种情况比较罕见。
  • 通过将数据库简化为简单的数据存储,您可以摆脱供应商锁定。
  • 如果做得好,您可以使用不同的数据库进行开发(可以是免费的 DB2 Express C),并为您的集成和生产环境提供更强大的数据库(例如 DB/2 for zOS)。这使您能够拥有更多的开发人员,而不必担心许可成本。

至于生成的模式等,实际上可能会发生四种工作流程:

  1. 对于设计,对象关系(而不是实体关系)图充当应用程序团队和数据库团队之间的契约。最终结果是 JPA 对象将在 DBA 设置的物理数据结构中运行。
  2. 对于Java应用程序开发,只要让每个开发人员拥有自己的数据库,让他们想炸多少就炸多少。 JPA 代码将为您生成架构。
  3. 对于数据库开发,生成的模式和类图将由 DBA 进行审查,以了解性能可以改进的地方。具体来说,它们用于指定 JPA 标准中不可用的索引,因为它不是跨数据库的。它们还用于设置表空间以及用于开发的所有访问控制和模式,但至少可以将结构的要点从它们中拿走并传递给应用程序团队,这使应用程序团队能够更灵活地适应改变。通常会发生的情况是 DBA 只包含一些生成的 SQL,然后进行更改以添加其他列和其他列,这些列将用于应用程序之外的其他目的(JPA 结构只需要应用程序所需的内容,它不需要一对一100%映射到数据库)
  4. 对于迁移,DBA需要在两个模式之间进行差异分析。有一个名为 dbsolo(非免费)的程序可以对大多数数据库执行此操作。但是,如果在JPA中完成,结构会更简单,因为理论上数据库上不再有任何业务逻辑,从而降低了由于升级而导致的数据迁移的复杂性。

最终结果是,您不能只是说您正在使用 JPA,而无需整个交付团队的参与,整个交付团队必须包括 DBA,他们愿意将数据结构的控制权和所有权交给应用程序团队,但仍是其中的一部分的设计和评论。

If this is an existing application, I would check what you have existing, if the database structure complex as can be seen with the DDL and the DDL shows significant logic is being done on the database itself, then you are better off using plain SQLs and let the DBA maintain your data structures. JPA does not really lend well when the database structures are already complicated and there is no business benefit to use JPA at that point.

What needs to happen is a project to migrate to JPA. There are a few advantages to that:

  • Business Logic is removed from the database layer (which is harder to scale horizontally) to the application tier.
  • Java developers are generally cheaper compared to a DBA. Though you still need someone who can do both database thinking and Java thinking to do this properly and that's more rare.
  • By reducing the database to become as simple datastore, you can break yourself from vendor lock-in.
  • If done right, you can have a different database for development (can be DB2 Express C which is free) and have a more robust database for your integration and production environments (e.g. DB/2 for zOS). This allows you to be able to have more developers without worrying about licensing costs as much.

As for schemas being generated and such, there are actually four workflows that can occur:

  1. For design, an Object-Relational (rather than an Entity-Relational) diagram serves as a contract between the application team and the database team. The end result is the JPA objects will run in the physical data structure that the DBA sets.
  2. For Java application development, just let each developer have their own database and let them blow it up as much as they want. The JPA code will generate the schemas for you.
  3. For database development, the generated schemas and class diagrams are passed onto review by the DBA to see where performance can be improved upon. Specifically they are there to specify the indices which are not available in the JPA standard since it is not cross database. They are also there to set up the table spaces and all the access controls and schemas for the development, but at least the gist of the structure can be taken away from them and passed onto the application team which gives the application team more flexibility to adapt to changes. What would normally happen is the DBA just includes some generated SQL and then have alters to add additional columns and others that would be used for other purposes outside of the application (the JPA structure needs only what is needed by the application, it does not need to map one-to-one 100% to the database)
  4. For migration, the DBA needs to do a differential analysis between the two schemas. There's a program called dbsolo (not free) that can do it with most databases. However, if things were done in JPA, the structures are simpler since in theory there is no longer any business logic on the database thus reducing the complexity of data migrations due to upgrades.

The net of it is you can't just say you're using JPA without involving the whole delivery team which will have to include the DBA willing to relinquish control and ownership of the structure of the data to the application team, but still be part of the design and reviews.

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