Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create sequence in h2 database

Tags:

sql

hibernate

h2

I want to create a sequence in h2 database for the below entity

public class Label {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "label_sequence")
  @SequenceGenerator(name = "label_sequence", sequenceName = "label_sequence", allocationSize = 100)
  private Long id;

  private String name;

  private String value;
}

Below is the sql command I am executing

CREATE SEQUENCE label_sequence
  START WITH 1
  INCREMENT BY 1
  MINVALUE 1;

I am getting the following error:

Syntax error in SQL statement "CREATE SEQUENCE LABEL_SEQUENCE
  START WITH 1
  INCREMENT BY 1
  MINVALUE[*] 1 "; SQL statement:
CREATE SEQUENCE label_sequence
  START WITH 1
  INCREMENT BY 1
  MINVALUE 1 [42000-140]

The below query works

CREATE SEQUENCE label_sequence
  START WITH 1
  INCREMENT BY 1;

But I am getting ID values less than 1 because of the allocation size I guess.

How can I make sure the ID values never go below 1?

like image 703
Phanindra Avatar asked Aug 08 '26 15:08

Phanindra


1 Answers

This should work:

CREATE SEQUENCE "MY_OWN_SEQ" 
MINVALUE 1 
MAXVALUE 999999999 
INCREMENT BY 1 
START WITH 202700 
NOCACHE 
NOCYCLE;
like image 182
ACV Avatar answered Aug 10 '26 10:08

ACV