diff --git a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/BasicDotIdentifierConsumer.java b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/BasicDotIdentifierConsumer.java index f8ef8229a5..01da33e4cc 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/BasicDotIdentifierConsumer.java +++ b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/BasicDotIdentifierConsumer.java @@ -205,17 +205,17 @@ public class BasicDotIdentifierConsumer implements DotIdentifierConsumer { final String prefix = path.substring( 0, splitPosition ); final String terminal = path.substring( splitPosition + 1 ); - final EnumJavaType enumType = jpaMetamodel.getEnumType( prefix ); - if ( enumType != null ) { - return new SqmEnumLiteral( - jpaMetamodel.enumValue( enumType, terminal ), - enumType, - terminal, - nodeBuilder - ); - } - try { + final EnumJavaType enumType = jpaMetamodel.getEnumType( prefix ); + if ( enumType != null ) { + return new SqmEnumLiteral( + jpaMetamodel.enumValue( enumType, terminal ), + enumType, + terminal, + nodeBuilder + ); + } + final Class namedClass = creationContext.getServiceRegistry() .requireService( ClassLoaderService.class ) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java index 9ef726a93a..f0e6a3d037 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java @@ -118,6 +118,7 @@ import org.hibernate.type.spi.TypeConfiguration; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import java.util.Objects; import java.util.Set; import java.util.UUID; @@ -286,6 +287,8 @@ public abstract class MockSessionFactory abstract boolean isEnum(String className); + abstract boolean isEnumConstant(String className, String terminal); + abstract boolean isFieldDefined(String qualifiedClassName, String fieldName); abstract boolean isConstructorDefined(String qualifiedClassName, List argumentTypes); @@ -843,6 +846,9 @@ public abstract class MockSessionFactory @Override public > E enumValue(EnumJavaType enumType, String enumValueName) { + if ( !isEnumConstant( enumType.getTypeName(), enumValueName ) ) { + throw new IllegalArgumentException( "No enum constant " + enumType.getTypeName() + "." + enumValueName ); + } return null; } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java index e0d7f45eaf..c7557aaf98 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java @@ -595,6 +595,18 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory { return typeElement != null && typeElement.getKind() == ElementKind.ENUM; } + @Override + boolean isEnumConstant(String className, String terminal) { + final TypeElement typeElement = elementUtil.getTypeElement(className); + if (typeElement == null || typeElement.getKind() != ElementKind.ENUM) { + return false; + } + return typeElement.getEnclosedElements() + .stream() + .filter(e -> terminal.equals(e.getSimpleName().toString())) + .anyMatch(e -> e.getKind() == ElementKind.ENUM_CONSTANT); + } + private static boolean isEmbeddableType(TypeElement type) { return hasAnnotation(type, "Embeddable"); }