From f0f1969228470c05435afb4bb07b0ea92b6e7d7a Mon Sep 17 00:00:00 2001 From: pedrobacchini Date: Fri, 15 Mar 2024 10:29:42 -0300 Subject: [PATCH] Validate enum with using naming strategies. --- docker-compose.yml | 38 +++++++++++++++ migrations/V1707239137__create_table.sql | 21 ++++++++ orm/hibernate-orm-6/pom.xml | 7 ++- .../org/hibernate/bugs/JPAUnitTestCase.java | 48 +++++++++++-------- .../hibernate/bugs/enums/ComposeStatus.java | 6 +++ .../java/org/hibernate/bugs/enums/Status.java | 6 +++ .../hibernate/bugs/model/ComposeSample.java | 34 +++++++++++++ .../java/org/hibernate/bugs/model/Sample.java | 34 +++++++++++++ .../test/resources/META-INF/persistence.xml | 11 +++-- 9 files changed, 176 insertions(+), 29 deletions(-) create mode 100644 docker-compose.yml create mode 100644 migrations/V1707239137__create_table.sql create mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/enums/ComposeStatus.java create mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/enums/Status.java create mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/model/ComposeSample.java create mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/model/Sample.java diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..1d5b0f33 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,38 @@ +version: '3.7' +services: + + postgres: + container_name: postgres + hostname: postgres + image: postgres:15.4 + environment: + POSTGRES_PASSWORD: 'rootroot' + POSTGRES_USER: 'root' + POSTGRES_DB: 'database' + ports: + - "5432:5432" + volumes: + - postgres:/var/lib/postgresql/data + restart: always + networks: + - bubble + + migrations: + image: flyway/flyway:9.16-alpine + platform: linux/amd64 + container_name: migrations + hostname: migrations + command: -url=jdbc:postgresql://postgres:5432/database -user=root -password=rootroot migrate + depends_on: + - postgres + volumes: + - ./migrations/:/flyway/sql/ + networks: + - bubble + +networks: + bubble: + external: true + +volumes: + postgres: diff --git a/migrations/V1707239137__create_table.sql b/migrations/V1707239137__create_table.sql new file mode 100644 index 00000000..527b161b --- /dev/null +++ b/migrations/V1707239137__create_table.sql @@ -0,0 +1,21 @@ +CREATE TYPE status AS ENUM ( + 'ACTIVE', + 'INACTIVE' +); + +CREATE TABLE sample +( + id BIGSERIAL NOT NULL, + status status NULL +); + +CREATE TYPE compose_status AS ENUM ( + 'ACTIVE', + 'INACTIVE' +); + +CREATE TABLE compose_sample +( + id BIGSERIAL NOT NULL, + compose_status compose_status NULL +); \ No newline at end of file diff --git a/orm/hibernate-orm-6/pom.xml b/orm/hibernate-orm-6/pom.xml index 2fdc392a..2a498404 100644 --- a/orm/hibernate-orm-6/pom.xml +++ b/orm/hibernate-orm-6/pom.xml @@ -24,11 +24,10 @@ hibernate-testing ${version.org.hibernate} - - com.h2database - h2 - ${version.com.h2database} + org.postgresql + postgresql + 42.6.1 junit diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java index 6662194d..64ab7528 100644 --- a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java @@ -3,7 +3,8 @@ import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; - +import org.hibernate.bugs.enums.Status; +import org.hibernate.bugs.model.Sample; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -13,26 +14,33 @@ */ public class JPAUnitTestCase { - private EntityManagerFactory entityManagerFactory; + private EntityManagerFactory entityManagerFactory; + + @Before + public void init() { + entityManagerFactory = Persistence.createEntityManagerFactory("templatePU"); + } - @Before - public void init() { - entityManagerFactory = Persistence.createEntityManagerFactory( "templatePU" ); - } + @After + public void destroy() { + entityManagerFactory.close(); + } - @After - public void destroy() { - entityManagerFactory.close(); - } + // Entities are auto-discovered, so just add them anywhere on class-path + // Add your tests, using standard JUnit. + /* + test executes correctly for mapping the enum with a simple name as in the case of the Sample class and a Status. + However, if you leave ComposeSample and ComposeStatus defined, the error is released. + Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: + Schema-validation: wrong column type encountered in column [compose_status] in table [compose_sample]; found [compose_status (Types#VARCHAR)], but expecting [composestatus (Types#NAMED_ENUM)] + */ + @Test + public void shouldValidateCorrectlyWithJustSampleMapping() { + EntityManager entityManager = entityManagerFactory.createEntityManager(); + entityManager.getTransaction().begin(); + entityManager.persist(new Sample(Status.ACTIVE)); + entityManager.getTransaction().commit(); + entityManager.close(); + } - // Entities are auto-discovered, so just add them anywhere on class-path - // Add your tests, using standard JUnit. - @Test - public void hhh123Test() throws Exception { - EntityManager entityManager = entityManagerFactory.createEntityManager(); - entityManager.getTransaction().begin(); - // Do stuff... - entityManager.getTransaction().commit(); - entityManager.close(); - } } diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/enums/ComposeStatus.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/enums/ComposeStatus.java new file mode 100644 index 00000000..d75e5b59 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/enums/ComposeStatus.java @@ -0,0 +1,6 @@ +package org.hibernate.bugs.enums; + +public enum ComposeStatus { + ACTIVE, + INACTIVE +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/enums/Status.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/enums/Status.java new file mode 100644 index 00000000..00c83c4f --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/enums/Status.java @@ -0,0 +1,6 @@ +package org.hibernate.bugs.enums; + +public enum Status { + ACTIVE, + INACTIVE +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/model/ComposeSample.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/model/ComposeSample.java new file mode 100644 index 00000000..95e6f0cb --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/model/ComposeSample.java @@ -0,0 +1,34 @@ +package org.hibernate.bugs.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import org.hibernate.annotations.JdbcType; +import org.hibernate.bugs.enums.ComposeStatus; +import org.hibernate.dialect.PostgreSQLEnumJdbcType; + +@Entity(name = "compose_sample") +public class ComposeSample { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Enumerated(EnumType.STRING) + @Column(name = "compose_status", nullable = false) + @JdbcType(PostgreSQLEnumJdbcType.class) + private ComposeStatus composeStatus; + + public ComposeSample() { + } + + + public ComposeSample(final ComposeStatus status) { + this.composeStatus = status; + } + +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/model/Sample.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/model/Sample.java new file mode 100644 index 00000000..36481b62 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/model/Sample.java @@ -0,0 +1,34 @@ +package org.hibernate.bugs.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import org.hibernate.annotations.JdbcType; +import org.hibernate.bugs.enums.Status; +import org.hibernate.dialect.PostgreSQLEnumJdbcType; + +@Entity(name = "sample") +public class Sample { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Enumerated(EnumType.STRING) + @Column(nullable = false) + @JdbcType(PostgreSQLEnumJdbcType.class) + private Status status; + + public Sample() { + } + + + public Sample(final Status status) { + this.status = status; + } + +} diff --git a/orm/hibernate-orm-6/src/test/resources/META-INF/persistence.xml b/orm/hibernate-orm-6/src/test/resources/META-INF/persistence.xml index 3c18b748..c3a4604b 100644 --- a/orm/hibernate-orm-6/src/test/resources/META-INF/persistence.xml +++ b/orm/hibernate-orm-6/src/test/resources/META-INF/persistence.xml @@ -13,16 +13,17 @@ - - - - + + + + + - +