21
21
import org .springframework .data .repository .query .QueryMethodEvaluationContextProvider ;
22
22
import org .springframework .data .repository .query .ResultProcessor ;
23
23
import org .springframework .data .repository .query .ReturnedType ;
24
+ import org .springframework .data .util .Lazy ;
24
25
import org .springframework .expression .spel .standard .SpelExpressionParser ;
25
26
import org .springframework .lang .Nullable ;
26
27
import org .springframework .util .Assert ;
39
40
abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
40
41
41
42
private final DeclaredQuery query ;
42
- private final DeclaredQuery countQuery ;
43
+ private final Lazy < DeclaredQuery > countQuery ;
43
44
private final QueryMethodEvaluationContextProvider evaluationContextProvider ;
44
45
private final SpelExpressionParser parser ;
45
46
private final QueryParameterSetter .QueryMetadataCache metadataCache = new QueryParameterSetter .QueryMetadataCache ();
@@ -69,9 +70,10 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
69
70
this .query = new ExpressionBasedStringQuery (queryString , method .getEntityInformation (), parser ,
70
71
method .isNativeQuery ());
71
72
72
- DeclaredQuery countQuery = query .deriveCountQuery (countQueryString , method .getCountQueryProjection ());
73
- this .countQuery = ExpressionBasedStringQuery .from (countQuery , method .getEntityInformation (), parser ,
74
- method .isNativeQuery ());
73
+ this .countQuery = Lazy .of (() -> {
74
+ DeclaredQuery countQuery = query .deriveCountQuery (countQueryString , method .getCountQueryProjection ());
75
+ return ExpressionBasedStringQuery .from (countQuery , method .getEntityInformation (), parser , method .isNativeQuery ());
76
+ });
75
77
76
78
this .parser = parser ;
77
79
@@ -117,7 +119,7 @@ protected ParameterBinder createBinder() {
117
119
@ Override
118
120
protected Query doCreateCountQuery (JpaParametersParameterAccessor accessor ) {
119
121
120
- String queryString = countQuery .getQueryString ();
122
+ String queryString = countQuery .get (). getQueryString ();
121
123
EntityManager em = getEntityManager ();
122
124
123
125
Query query = getQueryMethod ().isNativeQuery () //
@@ -142,7 +144,7 @@ public DeclaredQuery getQuery() {
142
144
* @return the countQuery
143
145
*/
144
146
public DeclaredQuery getCountQuery () {
145
- return countQuery ;
147
+ return countQuery . get () ;
146
148
}
147
149
148
150
/**
0 commit comments