Hibernate为PostgreSQL插入生成两个不同的序列ID [英] Hibernate generating two different sequence Ids for PostgreSQL insert

查看:145
本文介绍了Hibernate为PostgreSQL插入生成两个不同的序列ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用序列生成的主键定义的实体:

  @Id 
@GeneratedValue = GenerationType.SEQUENCE,generator =id_key_gen)
@SequenceGenerator(name =id_key_gen,sequenceName =id_key_seq)
@Column(name =id,unique = true,nullable = false )
public int getId(){
return this.id;
}

我使用的是PostgreSQL,而这个键被定义为一个串行。根据PostgreSQL

 从id_key_seq中选择last_value; 

退货

  1603。 

当我执行create()来保存此实体的实例时,我看到以下在我的日志里(编辑出不相关的东西):




05 15:15:26.948 org.hibernate.id.enhanced。 SequenceStructure [DEBUG] - 获得的序列值:1604

05 15:15:26.948 org.hibernate.event.def.AbstractSaveEventListener [DEBUG] - 生成的标识符:1554,使用策略:org.hibernate.id.enhanced.SequenceStyleGenerator




随后的SQL插入语句引用值1554,而不是它应该使用1604(根据从SequenceStructure返回的值,Hibernate是从哪里得到1554的?



在我看来,Hibernate有一个错误在这里 - SequenceStructure知道正确的下一个值,但它没有被使用。任何想法如何解决这个问题?



供参考:我知道这个页面,它说使用GenerationType.AUTO 是因为Hibernate人员完全搞砸了,但除此之外,有用的声明。

解决方案

看起来如果您使用GenerationType.SEQUENCE,则需要指定1的增量值以避免使用作为Hi / Lo种子的序列。



您发布的问题的第一个答案(有用的)解释了您需要在@GeneratedValue批注。

在较新的Hibernate版本中,您可以在Hibernate中设置 hibernate.id.new_generator_mappings = true 性能;请参阅文档


I have an entity defined with a sequence-generated primary key:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_key_gen")
@SequenceGenerator(name = "id_key_gen", sequenceName = "id_key_seq")
@Column(name = "id", unique = true, nullable = false)
public int getId() {
    return this.id;
}

I'm using PostgreSQL, and this key is defined as a serial. According to PostgreSQL

select last_value from id_key_seq;

returns

1603.

When I do a create() to persist an instance of this entity, I'm seeing the following in my logs (unrelated stuff edited out):


05 15:15:26.948 org.hibernate.id.enhanced.SequenceStructure [DEBUG] - Sequence value obtained: 1604

05 15:15:26.948 org.hibernate.event.def.AbstractSaveEventListener [DEBUG] - generated identifier: 1554, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator


Subsequent SQL insert statements refer to the value 1554, not what it should be using, 1604 (based on the value returned from the SequenceStructure. Where did Hibernate get 1554 from?

Seems to me Hibernate has a bug here - SequenceStructure knows the correct next value, but it isn't being used. Any idea how to resolve this?

FYI: I'm aware of this page, which says to use GenerationType.AUTO because the "Hibernate folks completely messed this up", but there isn't much beyond that not-very-helpful statement.

解决方案

Looks like if you use GenerationType.SEQUENCE, you need to be specifying an "increment value" of 1 to avoid it using the sequence as a Hi/Lo seed.

The first answer (the useful one) to the question you posted explains that you need to specify "allocationSize=1" in the @GeneratedValue annotation.

On newer Hibernate releases you can instead set hibernate.id.new_generator_mappings=true in your Hibernate properties; see the docs.

这篇关于Hibernate为PostgreSQL插入生成两个不同的序列ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆