3434import org .springframework .data .repository .query .parser .AbstractQueryCreator ;
3535import org .springframework .data .repository .query .parser .PartTree ;
3636import org .springframework .data .spel .EvaluationContextProvider ;
37+ import org .springframework .data .util .Lazy ;
3738import org .springframework .expression .EvaluationContext ;
3839import org .springframework .expression .spel .standard .SpelExpression ;
3940import org .springframework .lang .Nullable ;
5051 */
5152public class KeyValuePartTreeQuery implements RepositoryQuery {
5253
53- private final QueryMethodEvaluationContextProvider evaluationContextProvider ;
54+ private final Lazy < PartTree > partTree ;
5455 private final QueryMethod queryMethod ;
5556 private final KeyValueOperations keyValueOperations ;
57+ private final QueryMethodEvaluationContextProvider evaluationContextProvider ;
5658 private final QueryCreatorFactory <AbstractQueryCreator <KeyValueQuery <?>, ?>> queryCreatorFactory ;
5759
5860 /**
@@ -83,13 +85,16 @@ public KeyValuePartTreeQuery(QueryMethod queryMethod, QueryMethodEvaluationConte
8385 * @since 2.0
8486 */
8587 public KeyValuePartTreeQuery (QueryMethod queryMethod , QueryMethodEvaluationContextProvider evaluationContextProvider ,
86- KeyValueOperations keyValueOperations , QueryCreatorFactory queryCreatorFactory ) {
88+ KeyValueOperations keyValueOperations ,
89+ QueryCreatorFactory <AbstractQueryCreator <KeyValueQuery <?>, ?>> queryCreatorFactory ) {
8790
8891 Assert .notNull (queryMethod , "Query method must not be null!" );
8992 Assert .notNull (evaluationContextProvider , "EvaluationContextprovider must not be null!" );
9093 Assert .notNull (keyValueOperations , "KeyValueOperations must not be null!" );
9194 Assert .notNull (queryCreatorFactory , "QueryCreatorFactory type must not be null!" );
9295
96+ this .partTree = Lazy
97+ .of (() -> new PartTree (queryMethod .getName (), queryMethod .getEntityInformation ().getJavaType ()));
9398 this .queryMethod = queryMethod ;
9499 this .keyValueOperations = keyValueOperations ;
95100 this .evaluationContextProvider = evaluationContextProvider ;
@@ -130,17 +135,15 @@ protected Object doExecute(Object[] parameters, KeyValueQuery<?> query) {
130135 : keyValueOperations .count (query , queryMethod .getEntityInformation ().getJavaType ());
131136
132137 return new PageImpl (IterableConverter .toList (result ), page , count );
133-
134138 } else if (queryMethod .isCollectionQuery ()) {
135139
136140 return this .keyValueOperations .find (query , queryMethod .getEntityInformation ().getJavaType ());
137-
138141 } else if (queryMethod .isQueryForEntity ()) {
139142
140143 Iterable <?> result = this .keyValueOperations .find (query , queryMethod .getEntityInformation ().getJavaType ());
141144 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 ());
144147 }
145148
146149 throw new UnsupportedOperationException ("Query method not supported." );
@@ -206,8 +209,7 @@ private SpelExpression getSpelExpression(Object criteria) {
206209 */
207210 public KeyValueQuery <?> createQuery (ParameterAccessor accessor ) {
208211
209- PartTree tree = new PartTree (getQueryMethod ().getName (), getQueryMethod ().getEntityInformation ().getJavaType ());
210-
212+ PartTree tree = this .partTree .get ();
211213 AbstractQueryCreator <? extends KeyValueQuery <?>, ?> queryCreator = queryCreatorFactory .queryCreatorFor (tree ,
212214 accessor );
213215
@@ -235,7 +237,7 @@ public QueryMethod getQueryMethod() {
235237 * @author Christoph Strobl
236238 * @since 2.0
237239 */
238- public interface QueryCreatorFactory <T extends AbstractQueryCreator > {
240+ public interface QueryCreatorFactory <T extends AbstractQueryCreator <?, ?> > {
239241
240242 T queryCreatorFor (PartTree partTree , ParameterAccessor accessor );
241243 }
@@ -260,6 +262,7 @@ private static class ConstructorCachingQueryCreatorFactory
260262 }
261263
262264 @ Override
265+ @ SuppressWarnings ("unchecked" )
263266 public AbstractQueryCreator <KeyValueQuery <?>, ?> queryCreatorFor (PartTree partTree , ParameterAccessor accessor ) {
264267
265268 Assert .state (constructor != null ,
0 commit comments