34
34
import org .springframework .data .repository .query .parser .AbstractQueryCreator ;
35
35
import org .springframework .data .repository .query .parser .PartTree ;
36
36
import org .springframework .data .spel .EvaluationContextProvider ;
37
+ import org .springframework .data .util .Lazy ;
37
38
import org .springframework .expression .EvaluationContext ;
38
39
import org .springframework .expression .spel .standard .SpelExpression ;
39
40
import org .springframework .lang .Nullable ;
50
51
*/
51
52
public class KeyValuePartTreeQuery implements RepositoryQuery {
52
53
53
- private final QueryMethodEvaluationContextProvider evaluationContextProvider ;
54
+ private final Lazy < PartTree > partTree ;
54
55
private final QueryMethod queryMethod ;
55
56
private final KeyValueOperations keyValueOperations ;
57
+ private final QueryMethodEvaluationContextProvider evaluationContextProvider ;
56
58
private final QueryCreatorFactory <AbstractQueryCreator <KeyValueQuery <?>, ?>> queryCreatorFactory ;
57
59
58
60
/**
@@ -83,13 +85,16 @@ public KeyValuePartTreeQuery(QueryMethod queryMethod, QueryMethodEvaluationConte
83
85
* @since 2.0
84
86
*/
85
87
public KeyValuePartTreeQuery (QueryMethod queryMethod , QueryMethodEvaluationContextProvider evaluationContextProvider ,
86
- KeyValueOperations keyValueOperations , QueryCreatorFactory queryCreatorFactory ) {
88
+ KeyValueOperations keyValueOperations ,
89
+ QueryCreatorFactory <AbstractQueryCreator <KeyValueQuery <?>, ?>> queryCreatorFactory ) {
87
90
88
91
Assert .notNull (queryMethod , "Query method must not be null!" );
89
92
Assert .notNull (evaluationContextProvider , "EvaluationContextprovider must not be null!" );
90
93
Assert .notNull (keyValueOperations , "KeyValueOperations must not be null!" );
91
94
Assert .notNull (queryCreatorFactory , "QueryCreatorFactory type must not be null!" );
92
95
96
+ this .partTree = Lazy
97
+ .of (() -> new PartTree (queryMethod .getName (), queryMethod .getEntityInformation ().getJavaType ()));
93
98
this .queryMethod = queryMethod ;
94
99
this .keyValueOperations = keyValueOperations ;
95
100
this .evaluationContextProvider = evaluationContextProvider ;
@@ -130,17 +135,15 @@ protected Object doExecute(Object[] parameters, KeyValueQuery<?> query) {
130
135
: keyValueOperations .count (query , queryMethod .getEntityInformation ().getJavaType ());
131
136
132
137
return new PageImpl (IterableConverter .toList (result ), page , count );
133
-
134
138
} else if (queryMethod .isCollectionQuery ()) {
135
139
136
140
return this .keyValueOperations .find (query , queryMethod .getEntityInformation ().getJavaType ());
137
-
138
141
} else if (queryMethod .isQueryForEntity ()) {
139
142
140
143
Iterable <?> result = this .keyValueOperations .find (query , queryMethod .getEntityInformation ().getJavaType ());
141
144
return result .iterator ().hasNext () ? result .iterator ().next () : null ;
142
- } else if (new PartTree ( queryMethod . getName (), queryMethod . getEntityInformation (). getJavaType () ).isExistsProjection ()) {
143
- return keyValueOperations .count (query , queryMethod .getEntityInformation ().getJavaType ()) > 0 ;
145
+ } else if (partTree . get ( ).isExistsProjection ()) {
146
+ return keyValueOperations .exists (query , queryMethod .getEntityInformation ().getJavaType ());
144
147
}
145
148
146
149
throw new UnsupportedOperationException ("Query method not supported." );
@@ -206,8 +209,7 @@ private SpelExpression getSpelExpression(Object criteria) {
206
209
*/
207
210
public KeyValueQuery <?> createQuery (ParameterAccessor accessor ) {
208
211
209
- PartTree tree = new PartTree (getQueryMethod ().getName (), getQueryMethod ().getEntityInformation ().getJavaType ());
210
-
212
+ PartTree tree = this .partTree .get ();
211
213
AbstractQueryCreator <? extends KeyValueQuery <?>, ?> queryCreator = queryCreatorFactory .queryCreatorFor (tree ,
212
214
accessor );
213
215
@@ -235,7 +237,7 @@ public QueryMethod getQueryMethod() {
235
237
* @author Christoph Strobl
236
238
* @since 2.0
237
239
*/
238
- public interface QueryCreatorFactory <T extends AbstractQueryCreator > {
240
+ public interface QueryCreatorFactory <T extends AbstractQueryCreator <?, ?> > {
239
241
240
242
T queryCreatorFor (PartTree partTree , ParameterAccessor accessor );
241
243
}
@@ -260,6 +262,7 @@ private static class ConstructorCachingQueryCreatorFactory
260
262
}
261
263
262
264
@ Override
265
+ @ SuppressWarnings ("unchecked" )
263
266
public AbstractQueryCreator <KeyValueQuery <?>, ?> queryCreatorFor (PartTree partTree , ParameterAccessor accessor ) {
264
267
265
268
Assert .state (constructor != null ,
0 commit comments