From 3dc5896a08dd544c55f9358dcc8d7bbe0b8396fe Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 28 Mar 2024 20:29:22 +0100 Subject: [PATCH] handle inner enum literal in HQL Signed-off-by: Gavin King --- .../domain/internal/JpaMetamodelImpl.java | 27 ++++++++++++++----- .../hql/internal/SemanticQueryBuilder.java | 10 ++++++- 2 files changed, 29 insertions(+), 8 deletions(-) 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 d0ffaef2cb..231b81f77f 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 @@ -54,6 +54,7 @@ import org.hibernate.type.descriptor.java.EnumJavaType; import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.java.spi.DynamicModelJavaType; import org.hibernate.type.descriptor.java.spi.EntityJavaType; +import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry; import org.hibernate.type.spi.TypeConfiguration; import jakarta.persistence.EntityGraph; @@ -245,17 +246,29 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { @Override public EnumJavaType getEnumType(String prefix) { + final ClassLoaderService classLoaderService = + getServiceRegistry().requireService(ClassLoaderService.class); + final JavaTypeRegistry registry = getTypeConfiguration().getJavaTypeRegistry(); try { - final Class namedClass = - getServiceRegistry().requireService( ClassLoaderService.class ) - .classForName( prefix ); + final Class namedClass = classLoaderService.classForName( prefix ); if ( namedClass != null && namedClass.isEnum() ) { - return (EnumJavaType) getTypeConfiguration() - .getJavaTypeRegistry() - .resolveDescriptor(namedClass); + return (EnumJavaType) registry.resolveDescriptor(namedClass); } } - catch (Exception ignore) { + catch (ClassLoadingException classLoadingException) { + try { + final int lastDot = prefix.lastIndexOf('.'); + if ( lastDot>0) { + final String replaced = + prefix.substring(0, lastDot) + '$' + prefix.substring(lastDot+1); + final Class namedClass = classLoaderService.classForName( replaced ); + if ( namedClass != null && namedClass.isEnum() ) { + return (EnumJavaType) registry.resolveDescriptor(namedClass); + } + } + } + catch (ClassLoadingException ignore) { + } } return null; } diff --git a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java index 5cd6ab4590..038f0adfaa 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java @@ -2624,7 +2624,15 @@ public class SemanticQueryBuilder extends HqlParserBaseVisitor implem ctx = ctx.getChild( 0 ); if ( ctx instanceof HqlParser.SimplePathContext ) { - return ctx.getText(); + HqlParser.SimplePathContext simplePathContext = (HqlParser.SimplePathContext) ctx; + int size = simplePathContext.simplePathElement().size(); + if ( size==0 ) { + return simplePathContext.getText(); + } + else { + return simplePathContext.simplePathElement(size -1) + .identifier().getText(); + } } } }