40
40
import org .springframework .data .repository .query .Parameter ;
41
41
import org .springframework .data .repository .query .Parameters ;
42
42
import org .springframework .data .repository .query .QueryMethod ;
43
+ import org .springframework .data .repository .util .QueryExecutionConverters ;
43
44
import org .springframework .data .util .Lazy ;
45
+ import org .springframework .data .util .TypeInformation ;
44
46
import org .springframework .lang .Nullable ;
45
47
import org .springframework .util .Assert ;
46
48
import org .springframework .util .StringUtils ;
@@ -79,6 +81,7 @@ public class JpaQueryMethod extends QueryMethod {
79
81
80
82
private final QueryExtractor extractor ;
81
83
private final Method method ;
84
+ private final Class <?> returnType ;
82
85
83
86
private @ Nullable StoredProcedureAttributes storedProcedureAttributes ;
84
87
private final Lazy <LockModeType > lockModeType ;
@@ -107,6 +110,7 @@ protected JpaQueryMethod(Method method, RepositoryMetadata metadata, ProjectionF
107
110
Assert .notNull (extractor , "Query extractor must not be null!" );
108
111
109
112
this .method = method ;
113
+ this .returnType = potentiallyUnwrapReturnTypeFor (metadata , method );
110
114
this .extractor = extractor ;
111
115
this .lockModeType = Lazy
112
116
.of (() -> (LockModeType ) Optional .ofNullable (AnnotatedElementUtils .findMergedAnnotation (method , Lock .class )) //
@@ -126,8 +130,7 @@ protected JpaQueryMethod(Method method, RepositoryMetadata metadata, ProjectionF
126
130
return new JpaEntityGraph (entityGraph , getNamedQueryName ());
127
131
});
128
132
this .isNativeQuery = Lazy .of (() -> getAnnotationValue ("nativeQuery" , Boolean .class ));
129
- this .isCollectionQuery = Lazy
130
- .of (() -> super .isCollectionQuery () && !NATIVE_ARRAY_TYPES .contains (method .getReturnType ()));
133
+ this .isCollectionQuery = Lazy .of (() -> super .isCollectionQuery () && !NATIVE_ARRAY_TYPES .contains (this .returnType ));
131
134
this .isProcedureQuery = Lazy .of (() -> AnnotationUtils .findAnnotation (method , Procedure .class ) != null );
132
135
this .entityMetadata = Lazy .of (() -> new DefaultJpaEntityMetadata <>(getDomainClass ()));
133
136
@@ -136,6 +139,18 @@ protected JpaQueryMethod(Method method, RepositoryMetadata metadata, ProjectionF
136
139
assertParameterNamesInAnnotatedQuery ();
137
140
}
138
141
142
+ private static Class <?> potentiallyUnwrapReturnTypeFor (RepositoryMetadata metadata , Method method ) {
143
+
144
+ TypeInformation <?> returnType = metadata .getReturnType (method );
145
+
146
+ while (QueryExecutionConverters .supports (returnType .getType ())
147
+ || QueryExecutionConverters .supportsUnwrapping (returnType .getType ())) {
148
+ returnType = returnType .getRequiredComponentType ();
149
+ }
150
+
151
+ return returnType .getType ();
152
+ }
153
+
139
154
private void assertParameterNamesInAnnotatedQuery () {
140
155
141
156
String annotatedQuery = getAnnotatedQuery ();
@@ -243,7 +258,7 @@ QueryExtractor getQueryExtractor() {
243
258
* @return
244
259
*/
245
260
Class <?> getReturnType () {
246
- return method . getReturnType () ;
261
+ return returnType ;
247
262
}
248
263
249
264
/**
0 commit comments