Hibernate序列乘以50即可生成@Id? [英] Hibernate sequence gets multiplied by 50 for `@Id` generation?

查看:157
本文介绍了Hibernate序列乘以50即可生成@Id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private static final String SEQUENCE = "my_seq";    

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
@SequenceGenerator(name = SEQUENCE, sequenceName = SEQUENCE)
private Long titId;

这将创建以下架构:

CREATE SEQUENCE my_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1000
  CACHE 1;

观察: 当我将序列的current value设置为1时,自动生成的第一个@Id50.当我将值设置为1000时,第一个ID是50000.

Observation: When I set current value of the sequence to 1, then the first @Id autogenerated is 50. When I set the value to 1000, the first id is 50000.

因此,以某种方式,当前对序列的求值总是乘以50.为什么?如何防止这种情况,仅使用序列中的nexval?

So, somehow the current valuze of the sequence always gets multiplied by 50. Why? How can I prevent this and just use the nexval from the sequence?

推荐答案

此行为来自@SequenceGenerator,其参数allocationSize的默认值为50.您可以根据需要进行更改:

This behavior comes from @SequenceGenerator which has a default value of 50 for its parameter allocationSize. You can change it if you want :

@SequenceGenerator(name = SEQUENCE, sequenceName = SEQUENCE, allocationSize = 42)
private Long titId;

这是出于性能方面的考虑.它使Hibernate可以预订ID块,并防止每次需要新ID时都向数据库询问.

This is intended for performance reasons. It allows Hibernate to book a block of ids and prevents from asking the database each time you need a new id.

这篇关于Hibernate序列乘以50即可生成@Id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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