Skip to content

Commit 1e6b33b

Browse files
committed
Test explicitly defined generators when testing ORM 5.6 compatibility
Without the compatibility switch, we get the following failures. MariaDB: [ERROR] CompatibilityTest.sequence_defaultGenerator:38 1 expectation failed. Expected status code is <200> but was <500>. [ERROR] CompatibilityTest.sequence_genericGenerator_defaultAllocation:53 expected: 3L but was: -46L PostgreSQL: [ERROR] CompatibilityTest.sequence_defaultGenerator:38 1 expectation failed. Expected status code is <200> but was <500>. [ERROR] CompatibilityTest.sequence_genericGenerator_defaultAllocation:53 expected: 3L but was: -46L
1 parent 8dc4c1d commit 1e6b33b

File tree

14 files changed

+339
-11
lines changed

14 files changed

+339
-11
lines changed

integration-tests/hibernate-orm-compatibility-5.6/database-generator/src/main/java/io/quarkus/it/hibernate/compatibility/Main.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public int run(String... args) {
4747
entity.stringList = new ArrayList<>(List.of("one", "two"));
4848
entity.myEnum = MyEnum.VALUE2;
4949
em.persist(entity);
50+
51+
// Create more than one entity of each type,
52+
// so that we avoid the (uninteresting) edge case in sequence optimizers
53+
// where only 1 entity was created and the optimizer is just about to start another pool.
54+
em.persist(new MyEntity());
55+
em.persist(new MyEntityWithGenericGeneratorAndDefaultAllocationSize());
56+
em.persist(new MyEntityWithGenericGeneratorAndDefaultAllocationSize());
57+
em.persist(new MyEntityWithSequenceGeneratorAndDefaultAllocationSize());
58+
em.persist(new MyEntityWithSequenceGeneratorAndDefaultAllocationSize());
59+
5060
return entity;
5161
});
5262

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.quarkus.it.hibernate.compatibility;
2+
3+
import org.hibernate.annotations.GenericGenerator;
4+
import org.hibernate.annotations.Parameter;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
10+
@Entity(name = "myentity_gengendefallocsize")
11+
public class MyEntityWithGenericGeneratorAndDefaultAllocationSize {
12+
@Id
13+
@GeneratedValue(generator = "gengendefallocsize")
14+
@GenericGenerator(name = "gengendefallocsize", strategy = "sequence")
15+
public Long id;
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.quarkus.it.hibernate.compatibility;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
import javax.persistence.SequenceGenerator;
7+
8+
@Entity(name = "myentity_seqgendefallocsize")
9+
public class MyEntityWithSequenceGeneratorAndDefaultAllocationSize {
10+
@Id
11+
@GeneratedValue(generator = "seqgendefallocsize")
12+
@SequenceGenerator(name = "seqgendefallocsize")
13+
public Long id;
14+
15+
}

integration-tests/hibernate-orm-compatibility-5.6/mariadb/src/main/java/io/quarkus/it/hibernate/compatibility/CompatibilityTestResource.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,20 @@ public MyEntity create(MyEntity entity) {
3232
em.persist(entity);
3333
return entity;
3434
}
35+
36+
@POST
37+
@Path("/genericgenerator")
38+
public MyEntityWithGenericGeneratorAndDefaultAllocationSize create(
39+
MyEntityWithGenericGeneratorAndDefaultAllocationSize entity) {
40+
em.persist(entity);
41+
return entity;
42+
}
43+
44+
@POST
45+
@Path("/sequencegenerator")
46+
public MyEntityWithSequenceGeneratorAndDefaultAllocationSize create(
47+
MyEntityWithSequenceGeneratorAndDefaultAllocationSize entity) {
48+
em.persist(entity);
49+
return entity;
50+
}
3551
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.quarkus.it.hibernate.compatibility;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.Id;
6+
7+
import org.hibernate.annotations.GenericGenerator;
8+
9+
@Entity(name = "myentity_gengendefallocsize")
10+
public class MyEntityWithGenericGeneratorAndDefaultAllocationSize {
11+
@Id
12+
@GeneratedValue(generator = "gengendefallocsize")
13+
@GenericGenerator(name = "gengendefallocsize", strategy = "sequence")
14+
public Long id;
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.quarkus.it.hibernate.compatibility;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.Id;
6+
import jakarta.persistence.SequenceGenerator;
7+
8+
@Entity(name = "myentity_seqgendefallocsize")
9+
public class MyEntityWithSequenceGeneratorAndDefaultAllocationSize {
10+
@Id
11+
@GeneratedValue(generator = "seqgendefallocsize")
12+
@SequenceGenerator(name = "seqgendefallocsize")
13+
public Long id;
14+
15+
}

integration-tests/hibernate-orm-compatibility-5.6/mariadb/src/test/java/io/quarkus/it/hibernate/compatibility/CompatibilityTest.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,47 @@ public class CompatibilityTest {
3030
// https://github.com/hibernate/hibernate-orm/blob/6.0/migration-guide.adoc#implicit-identifier-sequence-and-table-name
3131
@Test
3232
@Order(1)
33-
public void sequence() {
33+
public void sequence_defaultGenerator() {
3434
var entity = new MyEntity();
3535
MyEntity createdEntity = given()
3636
.body(entity).contentType("application/json")
3737
.when().post("/compatibility/").then()
3838
.assertThat().statusCode(is(Status.OK.getStatusCode()))
3939
.extract().as(MyEntity.class);
40-
// For some reason the next val after a dump is 1001L, due to some MariaDB-specific caching stuff.
41-
// It doesn't matter much as it's caused by MariaDB;
42-
// we just want to check Hibernate ORM correctly retrieves the next sequence value.
43-
assertThat(createdEntity.id).isEqualTo(1001L);
40+
assertThat(createdEntity.id).isEqualTo(3L);
41+
}
42+
43+
// https://github.com/hibernate/hibernate-orm/blob/6.0/migration-guide.adoc#id-sequence-defaults
44+
@Test
45+
@Order(2)
46+
public void sequence_genericGenerator_defaultAllocation() {
47+
var entity = new MyEntityWithGenericGeneratorAndDefaultAllocationSize();
48+
MyEntityWithGenericGeneratorAndDefaultAllocationSize createdEntity = given()
49+
.body(entity).contentType("application/json")
50+
.when().post("/compatibility/genericgenerator").then()
51+
.assertThat().statusCode(is(Status.OK.getStatusCode()))
52+
.extract().as(MyEntityWithGenericGeneratorAndDefaultAllocationSize.class);
53+
assertThat(createdEntity.id).isEqualTo(3L);
54+
}
55+
56+
// https://github.com/hibernate/hibernate-orm/blob/6.0/migration-guide.adoc#id-sequence-defaults
57+
@Test
58+
@Order(2)
59+
public void sequence_sequenceGenerator_defaultAllocation() {
60+
var entity = new MyEntityWithSequenceGeneratorAndDefaultAllocationSize();
61+
MyEntityWithSequenceGeneratorAndDefaultAllocationSize createdEntity = given()
62+
.body(entity).contentType("application/json")
63+
.when().post("/compatibility/sequencegenerator").then()
64+
.assertThat().statusCode(is(Status.OK.getStatusCode()))
65+
.extract().as(MyEntityWithSequenceGeneratorAndDefaultAllocationSize.class);
66+
// Sequence generators defined through @SequenceGenerator have always defaulted to an allocation size of 50.
67+
// Since we've created 2 entities in Hibernate 5, we should be starting the second pool here, starting at 52.
68+
assertThat(createdEntity.id).isEqualTo(52L);
4469
}
4570

4671
// Just check that persisting with the old schema and new application does not throw any exception
4772
@Test
48-
@Order(2) // So that we can assert the generated ID in sequence()
73+
@Order(4) // So that we can assert the generated ID in sequence*()
4974
public void persistUsingOldSchema() {
5075
var entity = new MyEntity();
5176
entity.duration = Duration.of(59, ChronoUnit.SECONDS);

integration-tests/hibernate-orm-compatibility-5.6/mariadb/src/test/resources/db/migration/V1.0.0__orm5-6.sql

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
CREATE SEQUENCE `gengendefallocsize` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB;
2+
SELECT SETVAL(`gengendefallocsize`, 1001, 0);
13
CREATE SEQUENCE `hibernate_sequence` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB;
24
SELECT SETVAL(`hibernate_sequence`, 1001, 0);
5+
CREATE SEQUENCE `seqgendefallocsize` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 50 cache 1000 nocycle ENGINE=InnoDB;
6+
SELECT SETVAL(`seqgendefallocsize`, 50001, 0);
37
/*!40101 SET @saved_cs_client = @@character_set_client */;
48
/*!40101 SET character_set_client = utf8 */;
59
CREATE TABLE `MyEntity` (
@@ -15,4 +19,20 @@ CREATE TABLE `MyEntity` (
1519
PRIMARY KEY (`id`)
1620
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
1721
/*!40101 SET character_set_client = @saved_cs_client */;
18-
INSERT INTO `MyEntity` VALUES (1,59000000000,'2018-01-01 11:58:30.000000',0xACED0005757200025B494DBA602676EAB2A502000078700000000300000000000000010000002A,1,'2018-01-01 11:58:30.000000',0xACED0005737200136A6176612E7574696C2E41727261794C6973747881D21D99C7619D03000149000473697A657870000000027704000000027400036F6E6574000374776F78,0xF49C6BA88D7F417AA255D594DDDF729F0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,'2018-01-01 11:58:30.000000');
22+
INSERT INTO `MyEntity` VALUES (1,59000000000,'2018-01-01 11:58:30.000000',0xACED0005757200025B494DBA602676EAB2A502000078700000000300000000000000010000002A,1,'2018-01-01 11:58:30.000000',0xACED0005737200136A6176612E7574696C2E41727261794C6973747881D21D99C7619D03000149000473697A657870000000027704000000027400036F6E6574000374776F78,0xF49C6BA88D7F417AA255D594DDDF729F0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,'2018-01-01 11:58:30.000000'),(2,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
23+
/*!40101 SET @saved_cs_client = @@character_set_client */;
24+
/*!40101 SET character_set_client = utf8 */;
25+
CREATE TABLE `myentity_gengendefallocsize` (
26+
`id` bigint(20) NOT NULL,
27+
PRIMARY KEY (`id`)
28+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
29+
/*!40101 SET character_set_client = @saved_cs_client */;
30+
INSERT INTO `myentity_gengendefallocsize` VALUES (1),(2);
31+
/*!40101 SET @saved_cs_client = @@character_set_client */;
32+
/*!40101 SET character_set_client = utf8 */;
33+
CREATE TABLE `myentity_seqgendefallocsize` (
34+
`id` bigint(20) NOT NULL,
35+
PRIMARY KEY (`id`)
36+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
37+
/*!40101 SET character_set_client = @saved_cs_client */;
38+
INSERT INTO `myentity_seqgendefallocsize` VALUES (1),(2);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- MariaDB dumps lead to weird values in sequences because of caching mechanisms.
2+
-- => Reset sequences to the value they would have had, had we not dumped and restored the database.
3+
4+
ALTER SEQUENCE `hibernate_sequence` restart;
5+
-- We created exactly two entities
6+
SELECT nextval(`hibernate_sequence`);
7+
SELECT nextval(`hibernate_sequence`);
8+
9+
ALTER SEQUENCE `gengendefallocsize` restart;
10+
-- We created exactly two entities
11+
SELECT nextval(`gengendefallocsize`);
12+
SELECT nextval(`gengendefallocsize`);
13+
14+
ALTER SEQUENCE `seqgendefallocsize` restart;
15+
-- We created exactly two entities
16+
-- This sequence uses a pooled optimizer, but the first two entities still require two calls to nextval()
17+
SELECT nextval(`seqgendefallocsize`);
18+
SELECT nextval(`seqgendefallocsize`);

integration-tests/hibernate-orm-compatibility-5.6/postgresql/src/main/java/io/quarkus/it/hibernate/compatibility/CompatibilityTestResource.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,20 @@ public MyEntity create(MyEntity entity) {
3232
em.persist(entity);
3333
return entity;
3434
}
35+
36+
@POST
37+
@Path("/genericgenerator")
38+
public MyEntityWithGenericGeneratorAndDefaultAllocationSize create(
39+
MyEntityWithGenericGeneratorAndDefaultAllocationSize entity) {
40+
em.persist(entity);
41+
return entity;
42+
}
43+
44+
@POST
45+
@Path("/sequencegenerator")
46+
public MyEntityWithSequenceGeneratorAndDefaultAllocationSize create(
47+
MyEntityWithSequenceGeneratorAndDefaultAllocationSize entity) {
48+
em.persist(entity);
49+
return entity;
50+
}
3551
}

0 commit comments

Comments
 (0)