应避免使用哪些 orm.xml 功能以保持与数据库无关?
我正在开始 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JPA 功能
保留关键字
保持接近 SQL-92 标准
查询的翻译方式(尤其是本机查询)是宽松的。这在某些情况下很好,但有时会导致一些问题:
AS
SELECT *
=< /code> 表示相等,而不是
==
JPA features
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
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:
AS
in native queriesSELECT *
in native queries=
for equality and not==
我不熟悉 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.
一般来说,当选择“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.