Skip to content

Commit 9b04df8

Browse files
committed
Return parsed and rewritten query string from DefaultEntityQuery.
We now return the rewritten form of a query with all normalization and reformatting applied by QueryEnhancer. This allows a consistent representation of query strings. Closes #4034
1 parent 64dcdc1 commit 9b04df8

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/DefaultEntityQuery.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.jspecify.annotations.Nullable;
2222

23+
import org.springframework.data.util.Lazy;
24+
2325
/**
2426
* Encapsulation of a JPA query string, typically returning entities or DTOs. Provides access to parameter bindings.
2527
* <p>
@@ -40,11 +42,13 @@
4042
class DefaultEntityQuery implements EntityQuery, DeclaredQuery {
4143

4244
private final PreprocessedQuery query;
45+
private final Lazy<String> queryString;
4346
private final QueryEnhancer queryEnhancer;
4447

4548
DefaultEntityQuery(PreprocessedQuery query, QueryEnhancerFactory queryEnhancerFactory) {
4649
this.query = query;
4750
this.queryEnhancer = queryEnhancerFactory.create(query);
51+
this.queryString = Lazy.of(() -> queryEnhancer.getQuery().getQueryString());
4852
}
4953

5054
@Override
@@ -59,7 +63,7 @@ public boolean isNative() {
5963

6064
@Override
6165
public String getQueryString() {
62-
return query.getQueryString();
66+
return queryString.get();
6367
}
6468

6569
@Override

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/DefaultEntityQueryUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,10 @@ void reusesPositionalLikeBindingsWherePossible() {
281281
assertThat(query.getQueryString()).isEqualTo(
282282
"select u from User u where u.firstname like ?1 or u.firstname like ?2 or u.firstname like ?2 or u.firstname like ?1");
283283

284-
query = new TestEntityQuery("select u from User u where u.firstname like %?1 or u.firstname =?1", false);
284+
query = new TestEntityQuery("select u from User u where u.firstname like %?1 or u.firstname = ?1", false);
285285

286286
assertThat(query.hasParameterBindings()).isTrue();
287-
assertThat(query.getQueryString()).isEqualTo("select u from User u where u.firstname like ?1 or u.firstname =?2");
287+
assertThat(query.getQueryString()).isEqualTo("select u from User u where u.firstname like ?1 or u.firstname = ?2");
288288
}
289289

290290
@Test // GH-3041

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/SimpleJpaQueryUnitTests.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ class SimpleJpaQueryUnitTests {
9999
private ProjectionFactory factory = new SpelAwareProxyProjectionFactory();
100100

101101
@BeforeEach
102-
@SuppressWarnings({ "rawtypes", "unchecked" })
103102
void setUp() throws SecurityException, NoSuchMethodException {
104103

105104
when(em.getMetamodel()).thenReturn(metamodel);
@@ -148,7 +147,6 @@ void doesNotApplyPaginationToCountQuery() throws Exception {
148147
}
149148

150149
@Test
151-
@SuppressWarnings({ "rawtypes", "unchecked" })
152150
void discoversNativeQuery() throws Exception {
153151

154152
Method method = SampleRepository.class.getMethod("findNativeByLastname", String.class);
@@ -166,7 +164,6 @@ void discoversNativeQuery() throws Exception {
166164
}
167165

168166
@Test // GH-3155
169-
@SuppressWarnings({ "rawtypes", "unchecked" })
170167
void discoversNativeQueryFromNativeQueryInterface() throws Exception {
171168

172169
Method method = SampleRepository.class.getMethod("findByLastnameNativeAnnotation", String.class);
@@ -184,7 +181,6 @@ void discoversNativeQueryFromNativeQueryInterface() throws Exception {
184181
}
185182

186183
@Test // DATAJPA-352
187-
@SuppressWarnings("unchecked")
188184
void doesNotValidateCountQueryIfNotPagingMethod() throws Exception {
189185

190186
Method method = SampleRepository.class.getMethod("findByAnnotatedQuery");
@@ -346,7 +342,7 @@ void resolvesExpressionInCountQuery() throws Exception {
346342
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, factory, extractor);
347343

348344
AbstractJpaQuery jpaQuery = new SimpleJpaQuery(queryMethod, em,
349-
queryMethod.getDeclaredQuery("select u from User u"),
345+
queryMethod.getDeclaredQuery("select u from User /*some comment*/ u"),
350346
queryMethod.getDeclaredQuery("select count(u.id) from #{#entityName} u"), CONFIG);
351347
jpaQuery.createCountQuery(
352348
new JpaParametersParameterAccessor(queryMethod.getParameters(), new Object[] { PageRequest.of(1, 10) }));

0 commit comments

Comments
 (0)