diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractQueryMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractQueryMethod.java index 4eb7aaa32e..f567dbc369 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractQueryMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AbstractQueryMethod.java @@ -331,7 +331,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute { return returnTypeName; } - private void unwrapQuery(StringBuilder declaration, boolean unwrapped) { + void unwrapQuery(StringBuilder declaration, boolean unwrapped) { if ( !unwrapped ) { declaration .append("\n\t\t\t.unwrap(") diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java index fce88fa9ee..0791ca43b3 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java @@ -630,6 +630,7 @@ public class AnnotationMetaEntity extends AnnotationMeta { private static boolean isLegalGenericResultType(String containerTypeName) { return containerTypeName.equals(Constants.LIST) + || containerTypeName.equals(Constants.OPTIONAL) || containerTypeName.equals(Constants.TYPED_QUERY) || containerTypeName.equals(Constants.HIB_QUERY) || containerTypeName.equals(Constants.HIB_SELECTION_QUERY); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/CriteriaFinderMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/CriteriaFinderMethod.java index 35660c6b52..db9f424826 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/CriteriaFinderMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/CriteriaFinderMethod.java @@ -128,6 +128,11 @@ public class CriteriaFinderMethod extends AbstractFinderMethod { declaration .append(".getSingleResult()"); } + else if ( containerType.equals(Constants.OPTIONAL) ) { + unwrapQuery( declaration, unwrap ); + declaration + .append("\n\t\t\t.uniqueResultOptional()"); + } else if ( containerType.equals(Constants.LIST) ) { if ( unwrap || hasOrderParameter || hasEnabledFetchProfiles ) { declaration.append("\n\t\t\t"); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java index db3a7b46c1..65cd13efa0 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/QueryMethod.java @@ -152,6 +152,11 @@ public class QueryMethod extends AbstractQueryMethod { declaration .append("\n\t\t\t.getSingleResult()"); } + else if ( containerTypeName.equals(Constants.OPTIONAL) ) { + unwrapQuery( declaration, unwrapped ); + declaration + .append("\n\t\t\t.uniqueResultOptional()"); + } else if ( containerTypeName.equals(Constants.LIST) ) { declaration .append("\n\t\t\t.getResultList()"); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java index 7ee34b2967..4c03ac0bad 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/Constants.java @@ -100,6 +100,7 @@ public final class Constants { public static final String LIST = java.util.List.class.getName(); public static final String MAP = java.util.Map.class.getName(); public static final String SET = java.util.Set.class.getName(); + public static final String OPTIONAL = java.util.Optional.class.getName(); public static final Map COLLECTIONS = Map.of( COLLECTION, Constants.COLLECTION_ATTRIBUTE,