Java Hibernate 在使用序列时生成负 id 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9861416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 12:24:06  来源:igfitidea点击:

Hibernate generates negative id values when using a sequence

javahibernatejpajboss7.xjpa-2.0

提问by Tomer

I have a class with the following definition:

我有一个具有以下定义的类:

@Id
@SequenceGenerator(name = "SEQ_ACE_WORKERS_QUEUE_STATS_ID", sequenceName = "SEQ_ACE_WORKERS_QUEUE_STATS_ID", allocationSize = 500)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ACE_WORKERS_QUEUE_STATS_ID")
@Column(name = "ID")
private long Id;

When we ran it on Jboss 4.2.3 it worked fine and generated the proper ID's (starting from 1000+)

当我们在 Jboss 4.2.3 上运行它时,它运行良好并生成了正确的 ID(从 1000+ 开始)

Now we moved to jboss 7.1.1 and it generates negative ID's! (starting from -498 and going up)

现在我们转移到 jboss 7.1.1,它会生成负 ID!(从-498开始向上)

Any idea why this might happen?

知道为什么会发生这种情况吗?

采纳答案by jrm

I just ran into this issue when migrating from JBoss 6.1 to JBoss 7.1.

我刚从 JBoss 6.1 迁移到 JBoss 7.1 时遇到了这个问题。

According to the JBoss AS 7.1 JPA documentation ( https://docs.jboss.org/author/display/AS71/JPA+Reference+Guide#JPAReferenceGuide-Persistenceunitproperties),

根据 JBoss AS 7.1 JPA 文档(https://docs.jboss.org/author/display/AS71/JPA+Reference+Guide#JPAReferenceGuide-Persistenceunitproperties),

JBoss 7.1 automatically sets several hibernate properties. One of the properties being set is hibernate.id.new_generator_mappingswhich activates new ID generators that use different algorithms and are not backwards compatible. Setting this property to false in your persistence.xml file will restore the old ID generator behavior.

JBoss 7.1 会自动设置几个休眠属性。正在设置的属性之一是hibernate.id.new_generator_mappings激活使用不同算法且不向后兼容的新 ID 生成器。在您的persistence.xml 文件中将此属性设置为false 将恢复旧的ID 生成器行为。

The hibernate 4 documentation also has information regarding the new ID generators: http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html_single/#mapping-declaration-id-generator.

hibernate 4 文档还包含有关新 ID 生成器的信息:http: //docs.jboss.org/hibernate/core/4.0/manual/en-US/html_single/#mapping-declaration-id-generator

The hibernate documentation clearly states that the new ID generators are not enabled by default, but, as noted above, JBoss 7.1 is automatically enabling them.

hibernate 文档明确指出默认情况下未启用新的 ID 生成器,但如上所述,JBoss 7.1 会自动启用它们。

回答by falsarella

Setting hibernate.id.new_generator_mappingsto falsein my persistence.xmlwas just the first part of the solution to my problem:

设置hibernate.id.new_generator_mappingsfalsein mypersistence.xml只是解决我的问题的第一部分:

To completely solve the problem I added the allocationSizeto 1in the @SequenceGenerator(which I was omitting).

为了彻底解决问题我添加allocationSize1@SequenceGenerator(我被省略)。

回答by skay

The new behaviour is the followings:

新行为如下:

AllocationSizeis a range of primary key values reserved for Hibernate. And the select seq.nextvalfrom dual will be done only after hibernate consumed this range of primary keys.

AllocationSize是为 Hibernate 保留的一系列主键值。并且seq.nextval只有在休眠消耗了这个范围的主键后才会从双中选择。

So you mustdeclare the same value on both allocationSize(Hibernate) and sequence increment by(DB)

所以你必须allocationSize(Hibernate) 和序列increment by(DB)上声明相同的值

When explicitly set allocationSize=500, e.g. on Oracle

当显式设置时allocationSize=500,例如在 Oracle 上

create sequence SEQ_ACE_WORKERS_QUEUE_STATS_ID
       MINVALUE 1 
       MAXVALUE 999999999999999999999999999 
       START WITH 1
       INCREMENT BY 500 
       NOCACHE 
       NOCYCLE;

Otherwise, you'll notice negative values or constraint errors raised from your DB because of primary key collisions.

否则,您会注意到由于主键冲突而从数据库中引发的负值或约束错误。

When the app server is restarted, you'll notice the 'jump' between the latest primary key allocated, and the 'newly' sequence number selected at restart.

当应用服务器重新启动时,您会注意到分配的最新主键和重新启动时选择的“新”序列号之间的“跳转”。

Final comment: default value is 50. So if you don't specify allocationSizeon the Hibernate side, you mustdeclare increment by50 on the DB side.

最后评论:默认值是50。所以如果你allocationSize在Hibernate端没有指定,你必须increment by在DB端声明50。