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