Skip to content

Commit cff0724

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 fc6f18a commit cff0724

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, DeclaredQuery que
4848

4949
super(method, em, query, countQuery, queryConfiguration);
5050

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

5353
if (method.isPageQuery()) {
54-
validateQuery(getCountQuery().getQueryString(),
55-
String.format("Count query validation failed for method %s", method));
54+
validateQuery(getCountQuery(), "Count query %s validation failed for method %s", method);
5655
}
5756
}
5857

@@ -62,19 +61,24 @@ public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, DeclaredQuery que
6261
* @param query
6362
* @param errorMessage
6463
*/
65-
private void validateQuery(String query, String errorMessage, Object... arguments) {
64+
private void validateQuery(QueryProvider query, String errorMessage, JpaQueryMethod method) {
6665

6766
if (getQueryMethod().isProcedureQuery()) {
6867
return;
6968
}
7069

71-
try (EntityManager validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager()) {
72-
validatingEm.createQuery(query);
73-
} catch (RuntimeException e) {
70+
EntityManager validatingEm = null;
71+
var queryString = query.getQueryString();
72+
73+
try {
74+
validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager();
75+
validatingEm.createQuery(queryString);
76+
77+
} catch (RuntimeException e) {
7478

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

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
@@ -41,7 +41,6 @@
4141
import org.mockito.junit.jupiter.MockitoExtension;
4242
import org.mockito.junit.jupiter.MockitoSettings;
4343
import org.mockito.quality.Strictness;
44-
4544
import org.springframework.data.domain.Page;
4645
import org.springframework.data.domain.PageRequest;
4746
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)