JPA / Hibernate / Derby TableGenerator 使用负值
我希望数据库中生成的所有主键都是负整数。
我定义了一个 TableGenerator:
<table-generator name="MY_SEQ" table="MY_SEQUENCE_TABLE"
initial-value="-1000000" allocation-size="1" />
我假设这将从 -1000000 开始第一个生成的键,并将每个 id 加 1。因此下一个 id 将是 -999999。
当我创建并保留一个新实体时,出现以下错误:
java.sql.SQLIntegrityConstraintViolationException:该语句已中止,因为它会导致唯一或主键约束中出现重复的键值...
当我检查 MY_SEQUENCE_TABLE 值时SEQUENCE_NEXT_HIGH_VALUE 中的值是 2。我尝试在 ORM.xml 中定义表生成器并作为注释,但两种方式的行为是相同的。
是否可以在 Hibernate 中使用负值作为 PK?
谢谢
更新
我这样做是因为我正在将数据从一个数据库移动到另一个数据库。然后我将数据移回来。在此期间,可以在另一个数据库(Derby)中创建新实体。当我将实体移回原处时,我需要一种方法来判断它们是否是在该数据库上创建的,而不需要更改表架构。我想我会检查 id 是否有负值,并且我知道当我将数据移回时必须将其创建为新实体。
有谁知道 Hibernate 类生成什么来处理 TableGenerator 并生成序列的表代码?
更新 2
经过一些测试,Hibernate 在使用表生成器时似乎忽略了初始值和分配大小。
我将此属性添加到 persistence.xml 中,
<property name="hibernate.id.new_generator_mappings" value="true"/>
我切换到序列生成器,初始值已正确使用,但分配大小未保存为负值。根据德比规范,负序是有效的。
来自10.6参考:
如果指定,则 INCREMENT 值是适合 DataType 值的非零数字。如果未指定,则 INCREMENT 默认为 1。INCRMENT 是序列生成器前进的步长。如果 INCREMENT 为正数,则序列号会随着时间的推移而变大。如果 INCREMENT 为负数,则序列号变小。
去尝试更多的事情......
I want any generated primary keys in my database to be negative integers.
I defined a TableGenerator:
<table-generator name="MY_SEQ" table="MY_SEQUENCE_TABLE"
initial-value="-1000000" allocation-size="1" />
I assumed this would start the first generated key at -1000000 and increment each id by 1. So the next id would be -999999.
When I create and persist a new entity I get the following error:
java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint...
When I check the MY_SEQUENCE_TABLE the value in the SEQUENCE_NEXT_HIGH_VALUE is 2. I tried defining the table generator in the ORM.xml and as an annotation but the behavior is the same both ways.
Is it possible to use negative values for PKs in Hibernate?
Thanks
Update
I am doing this because I am moving data from one database to another. Then I am moving the data back. During this time new entities can get created in the other database (Derby). When I move the entities back I need a way to tell if they were created on that database without changing the table schema. I figured I would check the ids for negative values and they way I know that have to be created as new entities when I move the data back.
Does anyone know what Hibernate class generates handles the TableGenerator and generates the Table code for the sequence?
Update 2
After some testing it seems that Hibernate ignores initial-value and allocation-size when using table-generator.
I added this property to the persistence.xml
<property name="hibernate.id.new_generator_mappings" value="true"/>
I switched to a sequence generator and initial-value is properly used but allocation-size is not saved as a negative. According to the Derby spec a negative sequence is valid.
From the 10.6 reference:
If specified, the INCREMENT value is a non-zero number which fits in a DataType value. If not specified, the INCREMENT defaults to 1. INCREMENT is the step by which the sequence generator advances. If INCREMENT is positive, the sequence numbers get larger over time. If INCREMENT is negative, the sequence numbers get smaller.
Off to try more things.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我查看了JPA规范,似乎没有任何与关于初始值的约束,因此这可能取决于实现。含义:如果您希望符合标准,则不应使用负初始值。
您是否尝试过设置
allocation-size ="-1"
?我现在无法尝试这个,但我很好奇 Hibernate 的行为如何,因为数据库可以有负序列号。I took a look at JPA specification and there doesn't seem to be anything related to constraints regarding initial value, so this is probably implementation dependent. Meaning: you shouldn't use negative initial values if you want standards compliance.
Have you tried setting
allocation-size ="-1"
? I cannot try this out right now, but I'm curious how does Hibernate behave since databases can have negative sequence numbers.