Hibernate使用PostgreSQL序列不会影响序列表 [英] Hibernate use of PostgreSQL sequence does not affect sequence table

查看:488
本文介绍了Hibernate使用PostgreSQL序列不会影响序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已配置Hibernate使用PostgreSQL序列(通过注释)为主键 id 列生成值,如下所示:

<$ p $
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =pk_sequence)$ b @SequenceGenerator(name =pk_sequence,sequenceName =entity_id_seq)
@GeneratedValue $ b @Column(name =id,unique = true,nullable = false)
public int getId(){
return this.id;
}

我在这个配置中看到的是,hibernate已经分配了 id 在持续时间上的值大于3000,而在使用的序列上的查询显示如下:

  database =#select last_value from entity_id_seq; 
last_value
------------
69

(1 row)

问题:

是否有错误? hibernate与序列表同步吗?

如果不是,它会在哪里存储最后生成的ID?



谢谢。

解决方案

我有同样的问题。它与Hibernate的id分配策略有关。当您选择 .EQUENCE 时,Hibernate使用HiLo策略,默认分配ID为50的块。因此,您可以像这样明确地设置 allocationSize 值:

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

虽然我也听到了将HiLo策略与 allocationSize = 1 不是一个好习惯。有些人建议在需要处理数据库管理的序列时使用 GenerationType.AUTO



更新:我最终做了allocSize = 1,事情似乎按照我现在所期望的那样工作。我的应用程序是这样的,我不需要任何ID块,所以 YMMV 。 p>

I've configured Hibernate to use PostgreSQL sequence (via annotations) to generate values for primary key id column as follows:

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

What I see with this configuration is that hibernate is already assigning id values > 3000 on persisting, whereas the query on used sequence shows the following:

database=# select last_value from entity_id_seq;
last_value 
------------
     69

(1 row)

Questions:
Is there anything wrong or not?
Should hibernate sync with the sequence table?
If not, where does it store the last generated id?

Thank you.

解决方案

I had the same problem. It is related to the id allocating strategies of Hibernate. Whe n you choose GenerationType.SEQUENCE, Hibernate uses HiLo strategy which allocates IDs in blocks of 50 by default. So you can explicitly set allocationSize value like this:

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

Though, I've also heard opinions that using HiLo strategy with allocationSize=1 is not a good practice. Some people recommend to use GenerationType.AUTO instead when you have to deal with database-managed sequences

Update: I did end up going with allocationSize=1, and things seem to work as I expect now. My application is such that I don't really need blocks of IDs anyway, so YMMV.

这篇关于Hibernate使用PostgreSQL序列不会影响序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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