最佳实践:JPA 的最佳数据库命名约定?

发布于 2024-09-27 19:56:52 字数 1431 浏览 8 评论 0原文

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

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

发布评论

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

评论(4

余罪 2024-10-04 19:56:52

我应该遵循这两个标准并在 @Table 和 @Column 上使用 name 属性吗?或者我应该遵循 Java 约定并依赖默认的 JPA 映射。

如果 JPA 默认约定与您公司的首选约定不匹配(不存在“唯一正确的”标准),请覆盖它们。这可以使用 @Table@Column 注释来完成(在 Hibernate 的特定情况下,您还可以提供自己的 NamingStrategy 的实现)。

解决这种标准冲突的最常见方法和/或最佳方法是什么?

没有冲突,有 Java 命名约定,JPA 端有一个默认约定用于将对象映射到表(因为 JPA 必须选择一个)并且没有“ SQL 方面的一个真正的标准。所以:

  • 如果您的公司没有任何 SQL 命名约定,您可以使用 JPA 约定
    • 如果您不喜欢它们,请覆盖它们
  • 如果您的公司有约定,请遵循它们并覆盖 JPA 默认值

Should I follow both standards and use the name attribute on @Table and @Column? Or should I follow the Java conventions and rely on the default JPA mappings.

If the JPA default conventions don't match the preferred conventions of your company (there is no "one true" standard), override them. This can be done using the @Table and @Column annotations (in the particular case of Hibernate, you could also provide your own implementation of a NamingStrategy).

What is the most common approach and/or the best approach on this conflict of standards?

There is no conflict, there are Java naming conventions, there is one default convention on the JPA side for the mapping of objects to tables (because JPA had to pick one) and there is no "one true" standard on the SQL side. So:

  • if your company doesn't have any SQL naming conventions, you could use the JPA conventions
    • if you don't like them, override them
  • if your company has conventions in place, follow them and override the JPA defaults
喵星人汪星人 2024-10-04 19:56:52

我想这取决于您所指的约定。我不会将表名放入列名中 - 只是为了重复您已经知道的内容而丢失一半的命名空间有什么意义?我(尝试)遵循的(一些)规则是:

  1. 长而有意义的名称比短名称更好,例如 TRANSACTION_DATE 而不是 TRAN_DT。是的,当你只能使用 6 个字符的变量名时,我已经足够老了,可以编写 Fortran,而且我记得基本变体中只有 AZ、A0-Z0...A9-Z9 - 但我也足够老了学得更好。索引等的单字符变量名很好 - 事实上也是传统的 - 但是当我发现一个函数有 12 个单字母变量名,每个变量名都有多种用途时,我......不觉得好笑。

  2. 人工主键被命名为 ID_<<“表名称”>>。

  3. 单字段自然数据主键是最好的。两个字段的自然主键就可以了。三个或更多字段 - 创建一个人工主键并使自然键成为备用唯一键。

  4. 您永远、永远、永远都不能指望日期、时间或日期/时间字段是唯一的。曾经。别忘了这一点。我是认真的。

  5. 混淆编码技术相当于无能。

我确信还有更多,但这只是一个开始。恕我直言。 YMMV。

分享并享受。

I suppose that this depends on whose conventions you're referring to. I do not put the table name into the column name - what's the point of losing half your namespace just to repeat what you already know? (Some of) the rules I (try to) follow are:

  1. Long, meaningful names are better than short names, e.g. TRANSACTION_DATE rather than TRAN_DT. Yes, I'm old enough to have written Fortran when you were limited to 6-character variable names, and I recall Basic variants where you only had A-Z, A0-Z0...A9-Z9 - but I'm also old enough to have learned better. Single-character variable names for indices, etc, are fine - and in fact traditional - but when I find a function with twelve single-letter variable names each used for multiple purposes I...am not amused.

  2. Artificial primary keys are named ID_<<"name of table">>.

  3. Single-field natural data primary keys are best. Two-field natural primary keys are OK. Three or more fields - create an artificial primary key and make the natural key an alternate unique key.

  4. Thou shalt never, ever, ever count on a date, time, or date/time field to be unique. Ever. Don't forget this. I mean it.

  5. Obfuscatory coding techniques are equivalent to incompetence.

I'm sure there's more, but it's a start. All IMHO. YMMV.

Share and enjoy.

孤独岁月 2024-10-04 19:56:52

两者都遵循。数据库约定应该是为了 DBA 的缘故而存在的,并且在思维方式不同的情况下进行手动报告和查询。使用注释上的名称参数来实现此目的。

Follow both. The db convention should be there for DBA sake and manual reports and queries where the mind set is different. Use the name params on annotations to achieve this.

檐上三寸雪 2024-10-04 19:56:52

就我而言,两者都是可以接受的。但是,如果您决定不想要默认的驼峰式大小写,则可以获得不同的命名策略,而无需诉诸向每个注释添加名称属性的繁琐且容易出错的任务。

看一下 Hibernate 的 org.hibernate.cfg.ImprovedNamingStrategy 类。它使用下划线而不是驼峰式大小写。只需在 Hibernate 配置上设置一个属性即可使用它。

如果您确实愿意,您还可以扩展 ImprovementNamingStrategy 以在表名前面添加或全部大写,但这似乎没有必要。

As far as I'm concerned either are acceptable. But if you decide you don't want the default camel case, you CAN get a different naming strategy without resorting to the tedious and error-prone task of adding the name attribute to every annotation.

Take a look at Hibernate's org.hibernate.cfg.ImprovedNamingStrategy class. It uses underscores instead of camel case. It is simply a matter of setting a property on your Hibernate configuration to use it.

You could also extend the ImprovedNamingStrategy to prepend the table name or do all uppercase if you really want, but that seems unnecessary.

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