18
18
import jakarta .persistence .EntityManager ;
19
19
import jakarta .persistence .Query ;
20
20
21
- import org .apache .commons .logging .Log ;
22
- import org .apache .commons .logging .LogFactory ;
23
-
24
21
import org .springframework .data .domain .Pageable ;
25
22
import org .springframework .data .domain .Sort ;
26
23
import org .springframework .data .jpa .repository .QueryRewriter ;
27
24
import org .springframework .data .repository .query .QueryMethodEvaluationContextProvider ;
28
25
import org .springframework .data .repository .query .ResultProcessor ;
29
26
import org .springframework .data .repository .query .ReturnedType ;
27
+ import org .springframework .data .util .Lazy ;
30
28
import org .springframework .expression .spel .standard .SpelExpressionParser ;
31
29
import org .springframework .lang .Nullable ;
32
30
import org .springframework .util .Assert ;
46
44
abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
47
45
48
46
private final DeclaredQuery query ;
49
- private final DeclaredQuery countQuery ;
47
+ private final Lazy < DeclaredQuery > countQuery ;
50
48
private final QueryMethodEvaluationContextProvider evaluationContextProvider ;
51
49
private final SpelExpressionParser parser ;
52
50
private final QueryParameterSetter .QueryMetadataCache metadataCache = new QueryParameterSetter .QueryMetadataCache ();
@@ -65,8 +63,8 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
65
63
* @param queryRewriter must not be {@literal null}.
66
64
*/
67
65
public AbstractStringBasedJpaQuery (JpaQueryMethod method , EntityManager em , String queryString ,
68
- @ Nullable String countQueryString , QueryRewriter queryRewriter , QueryMethodEvaluationContextProvider evaluationContextProvider ,
69
- SpelExpressionParser parser ) {
66
+ @ Nullable String countQueryString , QueryRewriter queryRewriter ,
67
+ QueryMethodEvaluationContextProvider evaluationContextProvider , SpelExpressionParser parser ) {
70
68
71
69
super (method , em );
72
70
@@ -79,9 +77,10 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
79
77
this .query = new ExpressionBasedStringQuery (queryString , method .getEntityInformation (), parser ,
80
78
method .isNativeQuery ());
81
79
82
- DeclaredQuery countQuery = query .deriveCountQuery (countQueryString , method .getCountQueryProjection ());
83
- this .countQuery = ExpressionBasedStringQuery .from (countQuery , method .getEntityInformation (), parser ,
84
- method .isNativeQuery ());
80
+ this .countQuery = Lazy .of (() -> {
81
+ DeclaredQuery countQuery = query .deriveCountQuery (countQueryString , method .getCountQueryProjection ());
82
+ return ExpressionBasedStringQuery .from (countQuery , method .getEntityInformation (), parser , method .isNativeQuery ());
83
+ });
85
84
86
85
this .parser = parser ;
87
86
this .queryRewriter = queryRewriter ;
@@ -117,7 +116,7 @@ protected ParameterBinder createBinder() {
117
116
@ Override
118
117
protected Query doCreateCountQuery (JpaParametersParameterAccessor accessor ) {
119
118
120
- String queryString = countQuery .getQueryString ();
119
+ String queryString = countQuery .get (). getQueryString ();
121
120
EntityManager em = getEntityManager ();
122
121
123
122
Query query = getQueryMethod ().isNativeQuery () //
@@ -142,7 +141,7 @@ public DeclaredQuery getQuery() {
142
141
* @return the countQuery
143
142
*/
144
143
public DeclaredQuery getCountQuery () {
145
- return countQuery ;
144
+ return countQuery . get () ;
146
145
}
147
146
148
147
/**
0 commit comments