Skip to content

Commit eb5eaf6

Browse files
odrotbohmmp911de
authored andcommitted
Improve query method validation exceptions for declared queries.
When validating manually declared queries on repositories, the exception that captures the query to validate now actually also reports it in the exception message. Closes: #2736. Original pull request: #2738
1 parent ccd5daa commit eb5eaf6

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,34 @@ final class SimpleJpaQuery extends AbstractStringBasedJpaQuery {
4040
*
4141
* @param method must not be {@literal null}
4242
* @param em must not be {@literal null}
43-
* @param countQueryString
43+
* @param sourceQuery the original source query, must not be {@literal null} or empty.
4444
* @param queryRewriter must not be {@literal null}
4545
* @param valueExpressionDelegate must not be {@literal null}
4646
*/
47-
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, @Nullable String countQueryString,
47+
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, @Nullable String sourceQuery,
4848
QueryRewriter queryRewriter, ValueExpressionDelegate valueExpressionDelegate) {
49-
this(method, em, method.getRequiredAnnotatedQuery(), countQueryString, queryRewriter, valueExpressionDelegate);
49+
this(method, em, method.getRequiredAnnotatedQuery(), sourceQuery, queryRewriter, valueExpressionDelegate);
5050
}
5151

5252
/**
5353
* Creates a new {@link SimpleJpaQuery} that encapsulates a simple query string.
5454
*
5555
* @param method must not be {@literal null}
5656
* @param em must not be {@literal null}
57-
* @param queryString must not be {@literal null} or empty
57+
* @param sourceQuery the original source query, must not be {@literal null} or empty
5858
* @param countQueryString
5959
* @param queryRewriter
6060
* @param valueExpressionDelegate must not be {@literal null}
6161
*/
62-
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String queryString, @Nullable String countQueryString, QueryRewriter queryRewriter,
63-
ValueExpressionDelegate valueExpressionDelegate) {
62+
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String sourceQuery, @Nullable String countQueryString,
63+
QueryRewriter queryRewriter, ValueExpressionDelegate valueExpressionDelegate) {
6464

65-
super(method, em, queryString, countQueryString, queryRewriter, valueExpressionDelegate);
65+
super(method, em, sourceQuery, countQueryString, queryRewriter, valueExpressionDelegate);
6666

67-
validateQuery(getQuery().getQueryString(), "Validation failed for query for method %s", method);
67+
validateQuery(getQuery(), "Validation failed for query %s for method %s", method);
6868

6969
if (method.isPageQuery()) {
70-
validateQuery(getCountQuery().getQueryString(),
71-
String.format("Count query validation failed for method %s", method));
70+
validateQuery(getCountQuery(), "Count query %s validation failed for method %s", method);
7271
}
7372
}
7473

@@ -78,23 +77,24 @@ public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String queryStrin
7877
* @param query
7978
* @param errorMessage
8079
*/
81-
private void validateQuery(String query, String errorMessage, Object... arguments) {
80+
private void validateQuery(DeclaredQuery query, String errorMessage, JpaQueryMethod method) {
8281

8382
if (getQueryMethod().isProcedureQuery()) {
8483
return;
8584
}
8685

8786
EntityManager validatingEm = null;
87+
var queryString = query.getQueryString();
8888

8989
try {
9090
validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager();
91-
validatingEm.createQuery(query);
91+
validatingEm.createQuery(queryString);
9292

9393
} catch (RuntimeException e) {
9494

9595
// Needed as there's ambiguities in how an invalid query string shall be expressed by the persistence provider
96-
// https://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-07/message/17
97-
throw new IllegalArgumentException(String.format(errorMessage, arguments), e);
96+
// https://download.oracle.com/javaee-archive/jpa-spec.java.net/users/2012/07/0404.html
97+
throw new IllegalArgumentException(errorMessage.formatted(query, method), e);
9898

9999
} finally {
100100

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.mockito.junit.jupiter.MockitoExtension;
3939
import org.mockito.junit.jupiter.MockitoSettings;
4040
import org.mockito.quality.Strictness;
41-
4241
import org.springframework.data.domain.Page;
4342
import org.springframework.data.domain.PageRequest;
4443
import org.springframework.data.domain.Pageable;
@@ -194,14 +193,15 @@ void doesNotValidateCountQueryIfNotPagingMethod() throws Exception {
194193
}
195194

196195
@Test // DATAJPA-352
197-
@SuppressWarnings("unchecked")
198196
void validatesAndRejectsCountQueryIfPagingMethod() throws Exception {
199197

200198
Method method = SampleRepository.class.getMethod("pageByAnnotatedQuery", Pageable.class);
201199

202200
when(em.createQuery(Mockito.contains("count"))).thenThrow(IllegalArgumentException.class);
203201

204-
assertThatIllegalArgumentException().isThrownBy(() -> createJpaQuery(method)).withMessageContaining("Count")
202+
assertThatIllegalArgumentException() //
203+
.isThrownBy(() -> createJpaQuery(method)) //
204+
.withMessageContaining("Count") //
205205
.withMessageContaining(method.getName());
206206
}
207207

0 commit comments

Comments
 (0)