18
18
import jakarta .persistence .EntityManager ;
19
19
import jakarta .persistence .Query ;
20
20
21
- import java .util .function .Supplier ;
22
-
23
21
import org .apache .commons .logging .Log ;
24
22
import org .apache .commons .logging .LogFactory ;
23
+
25
24
import org .springframework .data .domain .Pageable ;
26
25
import org .springframework .data .domain .Sort ;
27
26
import org .springframework .data .jpa .repository .QueryRewriter ;
46
45
*/
47
46
abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
48
47
49
- private static final Log LOGGER = LogFactory .getLog (AbstractStringBasedJpaQuery .class );
50
-
51
48
private final DeclaredQuery query ;
52
49
private final DeclaredQuery countQuery ;
53
50
private final QueryMethodEvaluationContextProvider evaluationContextProvider ;
54
51
private final SpelExpressionParser parser ;
55
52
private final QueryParameterSetter .QueryMetadataCache metadataCache = new QueryParameterSetter .QueryMetadataCache ();
56
- private final Supplier < QueryRewriter > queryRewriterSupplier ;
53
+ private final QueryRewriter queryRewriter ;
57
54
58
55
/**
59
56
* Creates a new {@link AbstractStringBasedJpaQuery} from the given {@link JpaQueryMethod}, {@link EntityManager} and
@@ -65,16 +62,18 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
65
62
* @param countQueryString must not be {@literal null}.
66
63
* @param evaluationContextProvider must not be {@literal null}.
67
64
* @param parser must not be {@literal null}.
65
+ * @param queryRewriter must not be {@literal null}.
68
66
*/
69
67
public AbstractStringBasedJpaQuery (JpaQueryMethod method , EntityManager em , String queryString ,
70
- @ Nullable String countQueryString , QueryMethodEvaluationContextProvider evaluationContextProvider ,
71
- SpelExpressionParser parser , QueryRewriterProvider queryRewriterProvider ) {
68
+ @ Nullable String countQueryString , QueryRewriter queryRewriter , QueryMethodEvaluationContextProvider evaluationContextProvider ,
69
+ SpelExpressionParser parser ) {
72
70
73
71
super (method , em );
74
72
75
73
Assert .hasText (queryString , "Query string must not be null or empty!" );
76
74
Assert .notNull (evaluationContextProvider , "ExpressionEvaluationContextProvider must not be null!" );
77
75
Assert .notNull (parser , "Parser must not be null!" );
76
+ Assert .notNull (queryRewriter , "QueryRewriter must not be null!" );
78
77
79
78
this .evaluationContextProvider = evaluationContextProvider ;
80
79
this .query = new ExpressionBasedStringQuery (queryString , method .getEntityInformation (), parser ,
@@ -85,7 +84,7 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
85
84
method .isNativeQuery ());
86
85
87
86
this .parser = parser ;
88
- this .queryRewriterSupplier = queryRewriterProvider . of ( method ) ;
87
+ this .queryRewriter = queryRewriter ;
89
88
90
89
Assert .isTrue (method .isNativeQuery () || !query .usesJdbcStyleParameters (),
91
90
"JDBC style parameters (?) are not supported for JPA queries." );
@@ -169,20 +168,14 @@ protected Query createJpaQuery(String queryString, Sort sort, @Nullable Pageable
169
168
/**
170
169
* Use the {@link QueryRewriter}, potentially rewrite the query, using relevant {@link Sort} and {@link Pageable}
171
170
* information.
172
- *
171
+ *
173
172
* @param originalQuery
174
173
* @param sort
175
174
* @param pageable
176
175
* @return
177
176
*/
178
177
protected String potentiallyRewriteQuery (String originalQuery , Sort sort , @ Nullable Pageable pageable ) {
179
178
180
- QueryRewriter queryRewriter = this .queryRewriterSupplier .get ();
181
-
182
- if (queryRewriter == null ) {
183
- return originalQuery ;
184
- }
185
-
186
179
return pageable != null && pageable .isPaged () //
187
180
? queryRewriter .rewrite (originalQuery , pageable ) //
188
181
: queryRewriter .rewrite (originalQuery , sort );
0 commit comments