allow query and finder methods to return Optional

required by Jakarta Data spec
This commit is contained in:
Gavin King 2024-02-25 03:06:35 +01:00
parent 7e9b55ff92
commit 18bbbbb865
5 changed files with 13 additions and 1 deletions

View File

@ -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(")

View File

@ -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);

View File

@ -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");

View File

@ -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()");

View File

@ -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<String, String> COLLECTIONS = Map.of(
COLLECTION, Constants.COLLECTION_ATTRIBUTE,