From 2807cf61898eba88cb960729be53a94a11d47262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Thu, 28 Nov 2024 18:24:17 +0100 Subject: [PATCH 1/2] HHH-18894 Simplified test case selecting (non-managed) enum constant as column --- .../query/SelectUnknownEnumLiteralTest.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/query/SelectUnknownEnumLiteralTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/SelectUnknownEnumLiteralTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/SelectUnknownEnumLiteralTest.java new file mode 100644 index 000000000000..c5629d0a49c6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/SelectUnknownEnumLiteralTest.java @@ -0,0 +1,64 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.query; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Tuple; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@DomainModel(annotatedClasses = SelectUnknownEnumLiteralTest.Transaction.class) +@SessionFactory +public class SelectUnknownEnumLiteralTest { + + @BeforeAll + void setup(SessionFactoryScope scope) { + scope.inTransaction( em -> em.persist( new Transaction( 1L, "abc" ) ) ); + } + + @AfterAll + void clean(SessionFactoryScope scope) { + scope.inTransaction( em -> em.createMutationQuery( "delete from Tx" ).executeUpdate() ); + } + + @Test + void test(SessionFactoryScope scope) { + final List tuples = scope.fromSession( em -> + em.createQuery( + "SELECT org.hibernate.orm.test.query.SelectUnknownEnumLiteralTest$Type.TRANSACTION, e.id, e.reference FROM Tx e", + Tuple.class ).getResultList() ); + assertEquals( 1, tuples.size() ); + assertEquals( Type.TRANSACTION, tuples.get( 0 ).get( 0 ) ); + assertEquals( 1L, tuples.get( 0 ).get( 1 ) ); + } + + @Entity(name = "Tx") + static class Transaction { + @Id + Long id; + String reference; + + Transaction() { + } + + Transaction(Long id, String reference) { + this.id = id; + this.reference = reference; + } + } + + enum Type { + TRANSACTION, DIRECT_DEBIT_GROUP, DIRECT_DEBIT + } +} From dea55b0027ed058637146f16e07ba5cc6bbe4af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Thu, 28 Nov 2024 18:24:29 +0100 Subject: [PATCH 2/2] HHH-18894 Create and return new EnumJavaType object if class name represents enum that is not currently known --- .../model/domain/internal/JpaMetamodelImpl.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java index 12601134a37c..ba867dbca700 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java @@ -319,7 +319,21 @@ public Set> getEmbeddables() { @Override public EnumJavaType getEnumType(String className) { - return enumJavaTypes.get( className ); + final EnumJavaType enumJavaType = enumJavaTypes.get( className ); + if ( enumJavaType != null ) { + return enumJavaType; + } + final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class ); + try { + final Class clazz = classLoaderService.classForName( className ); + if ( clazz == null || !clazz.isEnum() ) { + return null; + } + return new EnumJavaType( clazz ); + } + catch (ClassLoadingException e) { + throw new RuntimeException( e ); + } } @Override