Hibernate 5序列生成问题 [英] Hibernate 5 Sequence Generate Issue

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

问题描述

我正在从3迁移到休眠5.我看到序列生成器在Hibernate 5中无法正常工作.我定义的序列的最小值为1000,增量为1.但是当我尝试创建新的实体记录时,我看到一条插入的ID为951的记录.似乎该ID与实际序列的下一个值相减50.在我的情况下,ID应该为1000.

I am migrating to hibernate 5 from 3. I am seeing the sequence generator not working properly in Hibernate 5. I have sequence defined with minimum value 1000 and increment by 1. But when I am trying to create a new entity record, I am seeing a record inserted with id 951. It seems like the id was minuses 50 from actual sequence next value. In my case the ID should be 1000.

请让我知道任何帮助.

这是我的实体和顺序:

实体:

@Entity
@Table(name = "SOME TABLE")
public class Group {

  @Id
  @Column(name = "id")
  @SequenceGenerator(name = "name",  sequenceName ="SEQ_name" )
  @GeneratedValue(strategy = GenerationType.AUTO, generator="name")
  private Long id;

  @Pattern(regexp = "^[^\\*]*$", message = "{3011}")
  @Size(message = "{3014}")
  @NotBlank(message = "{3000}")
  @Column(name = NAME, unique = true, nullable = false)
  private String name;

序列:

CREATE SEQUENCE  SEQ_name MINVALUE 1000 NOMAXVALUE INCREMENT BY 1 CACHE 20 NOORDER NOCYCLE;

推荐答案

Hibernate调用SEQ_name.nextval,但是如果allocationSize大于1,则会将其减小为distributionSize,然后将其增大为1.

Hibernate calls SEQ_name.nextval but if the allocationSize is greater than 1, it decrements it by the allocationSize and increments it by 1.

下一个(allocationSize-1)密钥的生成无需与数据库进行简单的通信(只需增加1)即可.

The next (allocationSize-1) key generation are done without communication with the database simple by increasing by 1.

因此,如果您使用默认的allocationSize(即50),则会有两种结果:

As a result, if you use the default allocationSize which is 50, there are two consequences:

序列必须将INCREMENT BY设置为与allocationSize

要将序列与数据库中的现有键对齐,请将START WITH设置为

To align the sequence with existing key in the database, set the START WITH to

最大(ID) +分配大小

这里有个小例子

-- START WITH = max(ID) + allocation size 
-- INCREMENT BY = allocation size
-- e.g. if 100 is the last key,
-- to start with a key 101 - set START WITH to 150
CREATE SEQUENCE hib_seq START WITH 150 INCREMENT BY 50;



select hib_seq.nextval - 50 + 1 from dual;
101
-- 49 times Hibernate performs increase by 1 - keys 102 to 150
-- new sequence generation
select hib_seq.nextval - 50 + 1 from dual;
151

这篇关于Hibernate 5序列生成问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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