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?
我正在开始JPA的冒险,并希望尽可能保持数据库不可知。我应该避免使用哪些orm.xml功能来保持数据库不可知?
For example, if I use strategy="AUTO"
in orm.xml as follows:
例如,如果我在orm.xml中使用strategy =“AUTO”,如下所示:
<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.
...然后MySQL将其显示为AUTO_INCREMENT列,如果我需要部署到Oracle,可能(我还不确定)会导致问题。
3 个解决方案
#1
1
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.
您可以使用所有JPA功能。更糟糕的是,您将需要更改注释或orm.xml(例如,如果您想在oracle中使用特殊序列),但所有功能都支持这种或那种方式而不会影响代码。这就是ORM的优点 - 你有一个额外的抽象层。
Reserved keywords
- Don't use reserved word to name your tables and columns
不要使用保留字来命名表和列
Keep close to SQL-92 standard
保持接近SQL-92标准
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
不要在本机查询中使用AS
切勿在本机查询中使用SELECT *
User =表示相等而不是==
仅使用SQL-92标准函数
#2
1
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.
我不熟悉JPA,但一般来说,合理的ORM应该与所有映射的数据库无关(对于主要数据库)。
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.
切换数据库时,必须处理现有数据的迁移问题。
#3
1
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.
通常,在选择“IDENTITY”的值生成时,以及在Sybase SERIAL上和DB2上等时,应使用MySQL“AUTO_INCREMENT”。等等。某些RDBMS没有等效的东西。
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.
“AUTO”的值生成是为了实现选择最适合该数据存储的内容。是的,在MySQL上,他们可以选择AUTO_INCREMENT,在Sybase SERIAL和Oracle SEQUENCE等上,但从用户代码的角度来看,一个人(应该)可以选择任何符合规范的实现。显然你不能再切换JPA实现并期望它使用完全相同的机制,因为JPA impl#1可以在MySQL上选择AUTO_INCREMENT,而JPA impl#2可以选择一些内部机制等。
#1
1
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.
您可以使用所有JPA功能。更糟糕的是,您将需要更改注释或orm.xml(例如,如果您想在oracle中使用特殊序列),但所有功能都支持这种或那种方式而不会影响代码。这就是ORM的优点 - 你有一个额外的抽象层。
Reserved keywords
- Don't use reserved word to name your tables and columns
不要使用保留字来命名表和列
Keep close to SQL-92 standard
保持接近SQL-92标准
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
不要在本机查询中使用AS
切勿在本机查询中使用SELECT *
User =表示相等而不是==
仅使用SQL-92标准函数
#2
1
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.
我不熟悉JPA,但一般来说,合理的ORM应该与所有映射的数据库无关(对于主要数据库)。
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.
切换数据库时,必须处理现有数据的迁移问题。
#3
1
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.
通常,在选择“IDENTITY”的值生成时,以及在Sybase SERIAL上和DB2上等时,应使用MySQL“AUTO_INCREMENT”。等等。某些RDBMS没有等效的东西。
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.
“AUTO”的值生成是为了实现选择最适合该数据存储的内容。是的,在MySQL上,他们可以选择AUTO_INCREMENT,在Sybase SERIAL和Oracle SEQUENCE等上,但从用户代码的角度来看,一个人(应该)可以选择任何符合规范的实现。显然你不能再切换JPA实现并期望它使用完全相同的机制,因为JPA impl#1可以在MySQL上选择AUTO_INCREMENT,而JPA impl#2可以选择一些内部机制等。