Skip to content

Commit 461e52d

Browse files
authored
[sequence-h2] h2 sequence (#18649)
1 parent 23ed71f commit 461e52d

File tree

6 files changed

+206
-0
lines changed

6 files changed

+206
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.h2seq;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
import jakarta.persistence.SequenceGenerator;
8+
import jakarta.persistence.Table;
9+
10+
@Entity
11+
@Table(name = "book")
12+
class Book {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_seq_gen")
16+
@SequenceGenerator(name = "book_seq_gen", sequenceName = "book_seq", allocationSize = 1)
17+
private Long id;
18+
private String title;
19+
20+
public Book() {
21+
}
22+
23+
public Book(String title) {
24+
this.title = title;
25+
}
26+
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
public void setId(Long id) {
32+
this.id = id;
33+
}
34+
35+
public String getTitle() {
36+
return title;
37+
}
38+
39+
public void setTitle(String title) {
40+
this.title = title;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.h2seq;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.domain.EntityScan;
6+
7+
@SpringBootApplication
8+
public class H2SeqDemoApplication {
9+
10+
public static void main(String... args) {
11+
SpringApplication.run(H2SeqDemoApplication.class, args);
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.h2seq;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.math.BigDecimal;
7+
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.context.ActiveProfiles;
13+
import org.springframework.test.context.junit.jupiter.SpringExtension;
14+
import org.springframework.transaction.annotation.Transactional;
15+
16+
import jakarta.persistence.EntityManager;
17+
18+
@ExtendWith(SpringExtension.class)
19+
@SpringBootTest(classes = H2SeqDemoApplication.class)
20+
@ActiveProfiles("h2-seq-oracle")
21+
@Transactional
22+
public class H2SeqAsOracleDemoIntegrationTest {
23+
24+
@Autowired
25+
private EntityManager entityManager;
26+
27+
@Test
28+
void whenCreateH2SequenceWithDefaultOptions_thenGetExpectedNextValueFromSequence() {
29+
entityManager.createNativeQuery("CREATE SEQUENCE my_seq")
30+
.executeUpdate();
31+
32+
String sqlNextValueFor = "SELECT NEXT VALUE FOR my_seq";
33+
BigDecimal nextValueH2 = (BigDecimal) entityManager.createNativeQuery(sqlNextValueFor)
34+
.getSingleResult();
35+
assertEquals(0, BigDecimal.ONE.compareTo(nextValueH2));
36+
37+
String sqlNextValueOralceStyle = "SELECT my_seq.nextval FROM dual";
38+
BigDecimal nextValueOracle = (BigDecimal) entityManager.createNativeQuery(sqlNextValueOralceStyle)
39+
.getSingleResult();
40+
assertEquals(0, BigDecimal.TWO.compareTo(nextValueOracle));
41+
42+
String sqlNextValueFunction = "SELECT nextval('my_seq')";
43+
nextValueOracle = (BigDecimal) entityManager.createNativeQuery(sqlNextValueFunction)
44+
.getSingleResult();
45+
assertEquals(0, BigDecimal.valueOf(3)
46+
.compareTo(nextValueOracle));
47+
48+
entityManager.createNativeQuery("DROP SEQUENCE my_seq")
49+
.executeUpdate();
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.h2seq;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.ActiveProfiles;
11+
import org.springframework.test.context.junit.jupiter.SpringExtension;
12+
import org.springframework.transaction.annotation.Transactional;
13+
14+
import jakarta.persistence.EntityManager;
15+
16+
@ExtendWith(SpringExtension.class)
17+
@SpringBootTest(classes = H2SeqDemoApplication.class)
18+
@ActiveProfiles("h2-seq")
19+
@Transactional
20+
public class H2SeqDemoIntegrationTest {
21+
22+
@Autowired
23+
private EntityManager entityManager;
24+
25+
private final String sqlNextValueFor = "SELECT NEXT VALUE FOR my_seq";
26+
private final String sqlNextValueFunction = "SELECT nextval('my_seq')";
27+
28+
@Test
29+
void whenCreateH2SequenceWithDefaultOptions_thenGetExpectedNextValueFromSequence() {
30+
entityManager.createNativeQuery("CREATE SEQUENCE my_seq")
31+
.executeUpdate();
32+
33+
Long nextValue = (Long) entityManager.createNativeQuery(sqlNextValueFunction)
34+
.getSingleResult();
35+
assertEquals(1, nextValue);
36+
37+
nextValue = (Long) entityManager.createNativeQuery(sqlNextValueFor)
38+
.getSingleResult();
39+
assertEquals(2, nextValue);
40+
41+
nextValue = (Long) entityManager.createNativeQuery(sqlNextValueFunction)
42+
.getSingleResult();
43+
assertEquals(3, nextValue);
44+
45+
entityManager.createNativeQuery("DROP SEQUENCE my_seq")
46+
.executeUpdate();
47+
}
48+
49+
@Test
50+
void whenCustomizeH2Sequence_thenGetExpectedNextValueFromSequence() {
51+
entityManager.createNativeQuery("CREATE SEQUENCE my_seq START WITH 1000 INCREMENT BY 10")
52+
.executeUpdate();
53+
54+
Long nextValue = (Long) entityManager.createNativeQuery(sqlNextValueFor)
55+
.getSingleResult();
56+
assertEquals(1000, nextValue);
57+
58+
nextValue = (Long) entityManager.createNativeQuery(sqlNextValueFunction)
59+
.getSingleResult();
60+
assertEquals(1010, nextValue);
61+
62+
nextValue = (Long) entityManager.createNativeQuery(sqlNextValueFor)
63+
.getSingleResult();
64+
assertEquals(1020, nextValue);
65+
66+
entityManager.createNativeQuery("DROP SEQUENCE my_seq")
67+
.executeUpdate();
68+
}
69+
70+
@Test
71+
void whenSaveEntityUsingSequence_thenCorrect() {
72+
entityManager.createNativeQuery("CREATE SEQUENCE book_seq")
73+
.executeUpdate();
74+
Book book1 = new Book("book1");
75+
assertNull(book1.getId());
76+
entityManager.persist(book1);
77+
assertEquals(1, book1.getId());
78+
79+
Book book2 = new Book("book2");
80+
entityManager.persist(book2);
81+
assertEquals(2, book2.getId());
82+
}
83+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring:
2+
datasource:
3+
driverClassName: org.h2.Driver
4+
url: jdbc:h2:mem:seqdb;MODE=Oracle
5+
username: sa
6+
password:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
spring:
2+
datasource:
3+
driverClassName: org.h2.Driver
4+
url: jdbc:h2:mem:seqdb
5+
username: sa
6+
password:
7+
jpa:
8+
hibernate:
9+
ddl-auto: none
10+
show-sql: true

0 commit comments

Comments
 (0)