handle inner enum literal in HQL

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-03-28 20:29:22 +01:00
parent ce317960fc
commit 3dc5896a08
2 changed files with 29 additions and 8 deletions

View File

@ -54,6 +54,7 @@ import org.hibernate.type.descriptor.java.EnumJavaType;
import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.spi.DynamicModelJavaType; import org.hibernate.type.descriptor.java.spi.DynamicModelJavaType;
import org.hibernate.type.descriptor.java.spi.EntityJavaType; import org.hibernate.type.descriptor.java.spi.EntityJavaType;
import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry;
import org.hibernate.type.spi.TypeConfiguration; import org.hibernate.type.spi.TypeConfiguration;
import jakarta.persistence.EntityGraph; import jakarta.persistence.EntityGraph;
@ -245,17 +246,29 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable {
@Override @Override
public EnumJavaType<?> getEnumType(String prefix) { public EnumJavaType<?> getEnumType(String prefix) {
final ClassLoaderService classLoaderService =
getServiceRegistry().requireService(ClassLoaderService.class);
final JavaTypeRegistry registry = getTypeConfiguration().getJavaTypeRegistry();
try { try {
final Class<?> namedClass = final Class<?> namedClass = classLoaderService.classForName( prefix );
getServiceRegistry().requireService( ClassLoaderService.class )
.classForName( prefix );
if ( namedClass != null && namedClass.isEnum() ) { if ( namedClass != null && namedClass.isEnum() ) {
return (EnumJavaType) getTypeConfiguration() return (EnumJavaType) registry.resolveDescriptor(namedClass);
.getJavaTypeRegistry()
.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; return null;
} }

View File

@ -2624,7 +2624,15 @@ public class SemanticQueryBuilder<R> extends HqlParserBaseVisitor<Object> implem
ctx = ctx.getChild( 0 ); ctx = ctx.getChild( 0 );
if ( ctx instanceof HqlParser.SimplePathContext ) { 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();
}
} }
} }
} }