问题描述
我有一个这样的jpa配置:
I have a jpa configuration like this:
@Id
//@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_gen")
@GeneratedValue(generator = "timedep_seq_gen")
@GenericGenerator(
name = "seq_gen",
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = {
@Parameter(name = "sequence_name", value = "sequence_myseq"),
@Parameter(name = "initial_value", value = "1"),
@Parameter(name = "increment_size", value = "10"),
@Parameter(name = "optimizer", value ="hilo")
}
)
private Long id;
插入操作正在创建ID值(例如1,2,3 ..),这很好,直到我手动执行
The insertions are creating id values like 1,2,3.. and this is fine till I manually do
SELECT nextval('sequence_myseq');
我希望从pgadmin(或任何其他客户端)运行以上命令时,jpa/hibernate生成器生成的下一组值应跳过id列的值,但事实并非如此.仍会生成这些值,而不会跳过任何id值.这是什么问题?
I expect that on running the above from pgadmin(or any other client), the next set of values generated by the jpa/hibernate generator should skip the values for the id column, but that is not the case. It still generates the values without skipping any id values. What is the problem here ?
编辑1 直到我得到一些具体的答案,看来hilo
优化将不适用于多个实例.以下工作正常,但需要您设置
EDIT 1Till I get some concrete answer, looks like hilo
optimization will not work for multiple instances. The following is working but it needs you to set
increment by 10
在您的序列定义中也是如此.
in your sequence definition as well.
@GeneratedValue(generator = "dep_seq_gen")
@SequenceGenerator(
name = "dep_seq_gen",
sequenceName = "sequence_dep",
allocationSize = 10
)
推荐答案
Hilo限制
Hilo算法无法与不知道hilo分配方案的系统进行互操作,这就是为什么Hibernate支持pooled
优化器的原因.
与hilo不同, pooled
优化器在应用程序分配的标识符值中包括数据库序列值.因此,任何新的序列值都不会与先前或将来的值冲突.
Unlike hilo, the pooled
optimizer includes the database sequence values in the identifier values allocated by the application. For this reason, any new sequence value is not going to conflict with previous or future values.
由于默认使用pooled
,因此您也可以使用SequenceGenerator
简化@Id
映射,而不是使用hilo
的更详细的@GenericGenerator
:
Since pooled
is used by default, you can also simplify your @Id
mapping using the SequenceGenerator
instead of the more verbose @GenericGenerator
you used for hilo
:
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sequence_myseq"
)
@SequenceGenerator(
name = "sequence_myseq",
sequenceName = "sequence_myseq",
allocationSize = 3
)
private Long id;
从hilo迁移到池化
如果使用hilo
并想迁移到pooled
优化器,则需要更改序列值,否则会产生冲突.
Migrating from hilo to pooled
If you used hilo
and want to migrate to the pooled
optimizer, you will need to change the sequence value as, otherwise, conflicts will be generated.
查看这篇文章有关如何进行迁移的更多详细信息从hilo
到pooled
优化器.
Check out this article for more details about how you can do the migration from hilo
to the pooled
optimizer.
这篇关于Hibernate hilo策略不会根据下一个数据库序列值生成值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!