Hibernate 5序列生成问题

Syd*_*abu 4 oracle jpa hibernate-5.x

我从3迁移到hibernate 5.我看到序列生成器在Hibernate 5中无法正常工作.我的序列定义为最小值1000并且递增1.但是当我尝试创建新的实体记录时,我看到了用id 951插入的记录.似乎id是实际序列下一个值的50.在我的情况下,ID应为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;
Run Code Online (Sandbox Code Playgroud)

序列:

CREATE SEQUENCE  SEQ_name MINVALUE 1000 NOMAXVALUE INCREMENT BY 1 CACHE 20 NOORDER NOCYCLE;
Run Code Online (Sandbox Code Playgroud)

Mar*_*ber 7

Hibernate调用SEQ_name.nextval但是如果allocationSize它大于1,它会通过allocationSize递减它并将其递增1.

下一个(allocationSize-1)密钥生成在没有与数据库通信的情况下通过增加1来完成.

因此,如果使用默认值allocationSize50,则会产生两种后果:

序列必须INCREMENT BY设置为相同的值 allocationSize

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

max(ID)+ allocationSize

这是一个小例子

-- 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
Run Code Online (Sandbox Code Playgroud)


小智 0

您定义的序列不是以 1000 开头,而是以 1(最小值 1)开头。我也没有看到您在哪里将提到的序列设置为实体使用的序列。