Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-17855: Validate enum with using naming strategies. #375

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
21 changes: 21 additions & 0 deletions migrations/V1707239137__create_table.sql
Original file line number Diff line number Diff line change
@@ -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
);
7 changes: 3 additions & 4 deletions orm/hibernate-orm-6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
<artifactId>hibernate-testing</artifactId>
<version>${version.org.hibernate}</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${version.com.h2database}</version>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.hibernate.bugs.enums;

public enum ComposeStatus {
ACTIVE,
INACTIVE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.hibernate.bugs.enums;

public enum Status {
ACTIVE,
INACTIVE
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
11 changes: 6 additions & 5 deletions orm/hibernate-orm-6/src/test/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>

<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/database"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="rootroot"/>

<property name="hibernate.connection.pool_size" value="5"/>

<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>

<property name="hibernate.max_fetch_depth" value="5"/>

Expand Down