JPA GenerationType.AUTO 不考虑具有自动增量的列 [英] JPA GenerationType.AUTO not considering column with auto increment
问题描述
我有一个表,其中包含一个简单的 int id 列,在 SQL Server 中具有标识自动增量.
I have a table with a simple int id column with Identity auto increment in SQL Server.
实体的 Id 用 @Id
和 @GeneratedValue
The entity's Id is annotated with @Id
and @GeneratedValue
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", length = 4, precision = 10, nullable = false)
private Integer id;
在 SQL Server 中,列被正确设置为标识,Seed
和 Increment
等于 1.
In SQL Server the column is properly set as Identity with Seed
and Increment
equals to 1.
当我尝试持久化此实体的实例时,Hibernate 尝试查询 hibernate_sequence 表以获取 ID 值.由于我还没有在我的架构中创建该表,所以我收到了一个错误:
When I try to persist an instance of this entity, Hibernate tries to query the hibernate_sequence table to obtain the ID value. Since I haven't created that table in my schema I'm getting an error:
无法读取 hi 值:com.microsoft.sqlserver.jdbc.SQLServerException:无效的对象名称MySchema.hibernate_sequence"
如果我将生成类型更改为 IDENTITY,则一切正常
If I change the generation type to IDENTITY everything works as expected
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", length = 4, precision = 10, nullable = false)
private Integer id;
我不能这样改变它,因为我的应用程序将在 MS SQL 和 ORACLE 上运行,而后者不支持自动递增的列.
I cannot change it this way, since my App will run both on MS SQL and ORACLE, and the latter does not support auto incremented columns.
据我所知,如果底层数据库支持自动增量行为,AUTO 类型应该使用自动增量行为,所以我不知道为什么不起作用.
As far as I know the AUTO type should use the auto increment behaviour if the underlying database has support to it, so I don't know why is not working.
更新:
我花了一些时间,但我能够准确地理解发生了什么.
It took me some time but I was able to understand exactly what is going on.
我正在使用具有以下行为的旧数据库:
I am working with legacy databases with the following behaviours:
- MSSQL:id 生成使用表 IDENTITY
- ORACLE:id 生成使用触发器.触发器查询并更新存储所有下一个 ID"的自定义表.此表称为 SEQ.
这是使用一些 id 生成策略的结果:
Here is the outcome of using some id generation strategies:
- AUTO:在 MSSQL 中不起作用,如上所述
- IDENTITY:适用于 MSSQL,但不受 Oracle 支持
- "native":在 MSSQL 中有效,但在 ORACLE 中失败.它失败是因为 Hibernate 激活了它的默认序列策略,它使用 hibernate_sequences.nextval.由于这是一个遗留应用程序,来自 SEQ 表(上面提到的)和 hibernate_sequences 的值不同步(该特定表的 SEQ 值是 6120,hibernate_sequences 是 1,这是预期的)因为它直到现在才被使用).
- AUTO: does not work in MSSQL, as explained above
- IDENTITY: works in MSSQL but is not supported by Oracle
- "native": works in MSSQL but fails in ORACLE. It fails because Hibernate activates its default sequence strategy, which uses hibernate_sequences.nextval. Since this is a legacy application the values from the SEQ table (mentioned above) and the hibernate_sequences are not synchronized (SEQ's value for that particular table is at 6120, and hibernate_sequences' is at 1, which is expected since it was not used until now).
所以我需要弄清楚的是一种配置该实体的方法:
So what I need to figure out is a way to configure that entity to:
- 使用 MSSQL 身份功能或
- 使用 Oracle 时,不要自动为 ID 变量设置任何值,并将所有内容留给预先存在的触发器
当我需要插入依赖于主实体的实体(通过外键)时,这可能会导致我在 Oracle 上出现严重问题,因为 Hibernate 不知道哪个 ID 值是由外部"触发器生成的.
This can cause me serious issues on Oracle when I need to insert entities that depend on the main entity (via foreign key), because Hibernate won't know which ID value was generated by the "external" trigger.
推荐答案
I had a similar problem and found this information (deeper explained in here).
将此属性添加到我的 persistence.xml 文件中修复了该问题:
Adding this property into my persistence.xml file fixed the issue:
<property name="hibernate.id.new_generator_mappings" value="false" />
这篇关于JPA GenerationType.AUTO 不考虑具有自动增量的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!