使用JPA / ORM生成db模式是个坏主意吗? [英] Is using JPA/ORM to generate a db schema a bad idea?

查看:184
本文介绍了使用JPA / ORM生成db模式是个坏主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Salve!

关于SO的其他问题/答案的一部分(以及声称相同的其他声明):

Part of another question/answer on SO (as well as other statements claiming the same):


如果您通过JPA更新数据库架构(通常不是一个好习惯)

if you are updating your database schema by JPA (generally not a good practice though)

您是否应该使用JPA实现来生成数据库模式?

无论如何,我必须自己模拟实体和关系。我还需要定义约束,例如notnull,主键和外键,数据类型和大小。

I have to model the entities and relationships ony my own anyways. I need to define constraints such as notnull, primary and foreign keys, data types and sizes as well.

假设正在使用的JPA实现中没有任何缺陷DDL模式创建代码并且假设我确实正确地指定了所有JPA约束,关系等,JPA实现创建的db模式应该完全相同 - 如果不是更好 - 作为我自己手工制作的模式,对吧?

Assuming that the JPA implementation in use does not have any flaws in its DDL schema creation code and also assuming that I do specify all JPA constraints, relations etc. correctly, the db schema created by the JPA implementation should be exactly the same -if not better- as a schema handcrafted by myself, right?

这不包括特殊情况,例如(业务逻辑)特定的INSERT触发器等,因为这些根本不能由JPA实现生成(据我所知,如果我错了,请纠正我。

您对此有何看法?

我目前正在做什么首先手工编写我的db模式,然后设置JPA约束,关系等,让JPA实现也创建一个db模式。然后我比较两个模式,看看我是否正确完成了设置。这当然意味着我还必须指定与我手工模式相同的列名等。

What's your view on this?
I do currently handcraft my db schema first, then setup the JPA constraints, relations etc. and let the JPA implementation create a db schema as well. I then compare the two schemata to see whether I've done the setup correctly. This does of course mean that I also have to specify the same column names etc. as I did for my handcrafted schema.

使我的问题更加精确;我并不盲目信任ORM框架来为我生成架构。我宁愿知道模式在我的脑海中如何看待,然后配置框架以匹配它。

我想你可以手工创建更多/最有效的模式,但毕竟我需要(或者而是希望将它们与ORM框架一起使用。因此,虽然我不应该这样做,但我仍然需要在创建数据库模式时记住ORM框架的限制。

To make my question more precise; I do not blindly trust on the ORM framework to generate a schema for me. I rather have an idea of how the schema looks in my mind and then configure the framework to match it.
I guess you could create more/most efficient schemata only by hand, but after all I need (or rather want to) use them with an ORM framework. So while I should not do so, I need to keep the limitations of ORM frameworks in mind when creating a db schema anyways.

因为我按顺序使用ORM框架不必关心RDBMS细节,让我的应用程序使用RDBMS特定的DDL创建模式有什么意义?

如果我必须使用一些现有的和异国情调的模式,我不需要(重新)创建模式以及我可能无法使用这样的通用工具作为ORM框架。

So since I use an ORM framework in order not to have to care about RDBMS specifics, what's the point of having my application create the schema using RDBMS specific DDL?
If I have to use some existing and exotic schema I do not need to (re-)create the schema anways as well as I might not be able to use such a generic tool as an ORM framework.

推荐答案

我通常让JPA最初只创建架构。之后,我对其进行微调并手动维护。

I usually let JPA create the schema initially only. After, I fine-tune it and maintain it by hand.

有几个原因我更喜欢手工维护架构:

There are several reasons I prefer maintaining the schema by hand:


  • 它允许在SQL代码中添加注释

  • 它允许为表和列添加注释/描述

  • 它允许指定表空间和JPA注释无法实现的其他东西

  • 它允许在几个SQL文件之间分割模式(每个表一个+约束,一个用于约束,例如)

  • 它允许我在模式迁移脚本中重用模式创建脚本的某些部分。例如,如果我的应用程序的第2版引入了3个新表,我需要一个可以重用创建三个新表的三个SQL文件的alter脚本

  • 我有时需要使用序列的同义词而不是具体的

  • 它让我选择主键约束的名称

  • 可能还有其他一些我忘记的原因

  • it allows putting comments in the SQL code
  • it allows adding comments/descriptions for the tables and columns
  • it allows specifying table spaces and other things that are not possible with JPA annotations
  • it allows splitting the schema creation between several SQL files (one per table + one for the constraints, for example)
  • it lets me reuse some parts of the schema creation script in my schema migration script. For example, if version 2 of my app introduces 3 new tables, I need an alter script that can reuse the three SQL files creating the three new tables
  • I sometimes need to use synonyms for sequences, rather than concrete ones
  • it lets me choose names for primary key constraints
  • probably some other reasons I've forgotten

这篇关于使用JPA / ORM生成db模式是个坏主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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