Skip to content

Commit 7ea0132

Browse files
committed
Polishing.
Refine error message format. See #2736 Original pull request: #2738
1 parent d854c63 commit 7ea0132

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String sourceQuer
6464

6565
super(method, em, sourceQuery, countQueryString, queryRewriter, valueExpressionDelegate);
6666

67-
validateQuery(getQuery(), "Validation failed for query %s for method %s", method);
67+
validateQuery(getQuery(), "Query '%s' validation failed for method %s", method);
6868

6969
if (method.isPageQuery()) {
70-
validateQuery(getCountQuery(), "Count query %s validation failed for method %s", method);
70+
validateQuery(getCountQuery(), "Count query '%s' validation failed for method %s", method);
7171
}
7272
}
7373

@@ -83,24 +83,14 @@ private void validateQuery(DeclaredQuery query, String errorMessage, JpaQueryMet
8383
return;
8484
}
8585

86-
EntityManager validatingEm = null;
87-
var queryString = query.getQueryString();
88-
89-
try {
90-
validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager();
86+
String queryString = query.getQueryString();
87+
try (EntityManager validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager()) {
9188
validatingEm.createQuery(queryString);
92-
9389
} catch (RuntimeException e) {
9490

9591
// Needed as there's ambiguities in how an invalid query string shall be expressed by the persistence provider
9692
// https://download.oracle.com/javaee-archive/jpa-spec.java.net/users/2012/07/0404.html
97-
throw new IllegalArgumentException(errorMessage.formatted(query, method), e);
98-
99-
} finally {
100-
101-
if (validatingEm != null) {
102-
validatingEm.close();
103-
}
93+
throw new IllegalArgumentException(errorMessage.formatted(queryString, method), e);
10494
}
10595
}
10696
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void doesNotValidateCountQueryIfNotPagingMethod() throws Exception {
192192
createJpaQuery(method);
193193
}
194194

195-
@Test // DATAJPA-352
195+
@Test // DATAJPA-352, GH-2736
196196
void validatesAndRejectsCountQueryIfPagingMethod() throws Exception {
197197

198198
Method method = SampleRepository.class.getMethod("pageByAnnotatedQuery", Pageable.class);
@@ -201,7 +201,7 @@ void validatesAndRejectsCountQueryIfPagingMethod() throws Exception {
201201

202202
assertThatIllegalArgumentException() //
203203
.isThrownBy(() -> createJpaQuery(method)) //
204-
.withMessageContaining("Count") //
204+
.withMessageContaining("User u") //
205205
.withMessageContaining(method.getName());
206206
}
207207

@@ -356,21 +356,21 @@ void resolvesExpressionInCountQuery() throws Exception {
356356
}
357357

358358
private AbstractJpaQuery createJpaQuery(Method method) {
359-
return createJpaQuery(method, null);
359+
return createJpaQuery(method, Optional.empty());
360360
}
361361

362-
private AbstractJpaQuery createJpaQuery(JpaQueryMethod queryMethod, @Nullable String queryString,
362+
private AbstractJpaQuery createJpaQuery(JpaQueryMethod queryMethod, String queryString,
363363
@Nullable String countQueryString) {
364364

365365
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(queryMethod, em, queryString, countQueryString,
366366
QueryRewriter.IdentityQueryRewriter.INSTANCE, ValueExpressionDelegate.create());
367367
}
368368

369-
private AbstractJpaQuery createJpaQuery(Method method, @Nullable Optional<String> countQueryString) {
369+
private AbstractJpaQuery createJpaQuery(Method method, Optional<String> countQueryString) {
370370

371371
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, factory, extractor);
372372
return createJpaQuery(queryMethod, queryMethod.getAnnotatedQuery(),
373-
countQueryString == null ? null : countQueryString.orElse(queryMethod.getCountQuery()));
373+
countQueryString.orElse(queryMethod.getCountQuery()));
374374
}
375375

376376
private String createQuery(AbstractStringBasedJpaQuery jpaQuery) {

0 commit comments

Comments
 (0)