allow query and finder methods to return Stream

required by Jakarta Data spec
This commit is contained in:
Gavin King 2024-02-25 03:23:47 +01:00
parent 18bbbbb865
commit b039eecd8e
7 changed files with 24 additions and 4 deletions

View File

@ -99,6 +99,8 @@ import static java.lang.annotation.RetentionPolicy.CLASS;
* or one of the following types:
* <ul>
* <li>{@link java.util.List java.util.List&lt;E&gt;},
* <li>{@link java.util.stream.Stream java.util.stream.Stream&lt;E&gt;},
* <li>{@link java.util.Optional java.util.Optional&lt;E&gt;},
* <li>{@code io.smallrye.mutiny.Uni<E>}, when used with Hibernate Reactive,
* <li>{@link org.hibernate.query.Query org.hibernate.query.Query&lt;E&gt;},
* <li>{@link org.hibernate.query.SelectionQuery org.hibernate.query.SelectionQuery&lt;E&gt;},

View File

@ -81,8 +81,13 @@ import static java.lang.annotation.RetentionPolicy.CLASS;
* For a {@code select} query, the return type of an annotated method
* must be:
* <ul>
* <li>an entity type, or {@link java.util.List List&lt;E&gt;} where
* {@code E} is an entity type,
* <li>an entity type, or {@link java.util.Optional Optional&lt;E&gt;}
* where {@code E} is an entity type,
* <li>{@link java.util.List List&lt;E&gt;} or
* {@link java.util.stream.Stream Stream&lt;E&gt;}
* where {@code E} is an entity type,
* <li>{@link java.util.List List&lt;Object[]&gt;} or
* {@link java.util.stream.Stream Stream&lt;Object[]&gt;}
* <li>a record type or JavaBean class with a constructor signature
* matching the types in the query {@code select} list, or
* {@link java.util.List List&lt;R&gt;} where {@code R} is such

View File

@ -77,8 +77,8 @@ import static java.lang.annotation.RetentionPolicy.CLASS;
* <p>
* The return type of an annotated method must be:
* <ul>
* <li>an entity type,
* <li>{@link java.util.List},
* <li>an entity type or {@link java.util.Optional},
* <li>{@link java.util.List} or {@link java.util.stream.Stream},
* <li>{@code io.smallrye.mutiny.Uni}, when used with Hibernate Reactive,
* <li>{@link org.hibernate.query.Query},
* <li>{@link jakarta.persistence.Query}, or

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.STREAM)
|| containerTypeName.equals(Constants.OPTIONAL)
|| containerTypeName.equals(Constants.TYPED_QUERY)
|| containerTypeName.equals(Constants.HIB_QUERY)

View File

@ -133,6 +133,13 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
declaration
.append("\n\t\t\t.uniqueResultOptional()");
}
else if ( containerType.equals(Constants.STREAM) ) {
if ( unwrap || hasOrderParameter || hasEnabledFetchProfiles ) {
declaration.append("\n\t\t\t");
}
declaration
.append(".getResultStream()");
}
else if ( containerType.equals(Constants.LIST) ) {
if ( unwrap || hasOrderParameter || hasEnabledFetchProfiles ) {
declaration.append("\n\t\t\t");

View File

@ -157,6 +157,10 @@ public class QueryMethod extends AbstractQueryMethod {
declaration
.append("\n\t\t\t.uniqueResultOptional()");
}
else if ( containerTypeName.equals(Constants.STREAM) ) {
declaration
.append("\n\t\t\t.getResultStream()");
}
else if ( containerTypeName.equals(Constants.LIST) ) {
declaration
.append("\n\t\t\t.getResultList()");

View File

@ -101,6 +101,7 @@ public final class Constants {
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 String STREAM = java.util.stream.Stream.class.getName();
public static final Map<String, String> COLLECTIONS = Map.of(
COLLECTION, Constants.COLLECTION_ATTRIBUTE,