应避免使用哪些 orm.xml 功能以保持与数据库无关?

发布于 2024-08-22 10:50:18 字数 324 浏览 6 评论 0原文

我正在开始 JPA 的冒险,并希望尽可能保持与数据库无关。我应该避免使用哪些 orm.xml 功能来保持与数据库无关?

例如,如果我在 orm.xml 中使用 strategy="AUTO" 如下:

<id name="id">
    <generated-value strategy="AUTO" />
</id>

...然后 MySQL 将其显示为 AUTO_INCRMENT 列,这可能(我不是但肯定的是)如果我需要部署到 Oracle,会导致问题。

I'm embarking on an adventure in JPA and want to, inasmuch as possible, stay database agnostic. What orm.xml features should I avoid to stay database agnostic?

For example, if I use strategy="AUTO" in orm.xml as follows:

<id name="id">
    <generated-value strategy="AUTO" />
</id>

...then MySQL shows it as an AUTO_INCREMENT column which may (I'm not yet sure) cause problems if I needed to deploy to Oracle.

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

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

发布评论

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

评论(3

秋意浓 2024-08-29 10:50:18

JPA 功能

  • 您可以使用所有 JPA 功能。最糟糕的是,您将需要更改注释或orm.xml(例如,如果您想在oracle中使用特殊序列),但所有功能都以一种或另一种方式支持,而不影响代码。这就是 ORM 的好处——你有一个额外的抽象层。

保留关键字

  • 不要使用保留字来命名表和列

保持接近 SQL-92 标准

查询的翻译方式(尤其是本机查询)是宽松的。这在某些情况下很好,但有时会导致一些问题:

  • 不要在本机查询中使用 AS
  • 切勿在本机查询中使用 SELECT *
  • User =< /code> 表示相等,而不是 ==
  • 仅使用 SQL-92 标准函数

JPA features

  • You can use all JPA features. At worse you will need to change the annotations or orm.xml (e.g. if you want to use a special sequence in oracle), but all features are supported one way or another without impacting the code. That's what's nice with an ORM -- you have an extra-layer of abstraction.

Reserved keywords

  • Don't use reserved word to name your tables and columns

Keep close to SQL-92 standard

The way the queries are translated (especially the native ones) is loose. This is great in some case, but can lead to some problems sometimes:

  • Don't use AS in native queries
  • Never use SELECT * in native queries
  • User = for equality and not ==
  • Use only the SQL-92 standard functions
层林尽染 2024-08-29 10:50:18

我不熟悉 JPA,但一般来说,合理的 ORM 的所有映射都应该与数据库无关(对于主要数据库)。

特别是“自动”增量策略应该开箱即用。

切换数据库时,您必须处理现有数据的迁移问题。

I am not familiar with JPA, but in general a reasonable ORM should be database agnostic (for the the major databases) for all of its mappings.

Especially an "AUTO" Increment strategy should work out of the box..

When switching the database, you have to deal with migration issues for the existing data.

微凉徒眸意 2024-08-29 10:50:18

一般来说,当选择“IDENTITY”的值生成时,以及在 Sybase SERIAL 和 DB2 等上,应使用 MySQL“AUTO_INCRMENT”。某些 RDBMS 没有等效的东西。

“AUTO”的值生成是为了让实现选择最适合该数据存储的值。是的,在 MySQL 上,他们可以选择 AUTO_INCRMENT,在 Sybase SERIAL 上,在 Oracle SEQUENCE 上,等等,但从用户代码的角度来看,人们将(应该)在任何符合规范的实现上工作。显然,您不能切换 JPA 实现并期望它使用完全相同的机制,因为 JPA impl #1 可能会在 MySQL 上选择 AUTO_INCRMENT,而 JPA impl #2 可能会选择某些内部机制等。

In general MySQL "AUTO_INCREMENT" should be used when selecting value generation of "IDENTITY", and on Sybase SERIAL, and on DB2 ... etc. Some RDBMS don't have something equivalent.

Value generation of "AUTO" is for the implementation to choose what is best for that datastore. Yes, on MySQL they may choose AUTO_INCREMENT, and on Sybase SERIAL, and on Oracle SEQUENCE, etc etc, but from the user code point of view that one will (should) work on any spec-compliant implementation. Obviously you cannot then switch JPA implementations and expect it to use the exact same mechanism, since JPA impl #1 may choose AUTO_INCREMENT on MySQL, and JPA impl #2 may choose some internal mechanism etc etc.

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