Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve query method validation exceptions for declared queries. #2738

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,39 @@ final class SimpleJpaQuery extends AbstractStringBasedJpaQuery {
*
* @param method must not be {@literal null}
* @param em must not be {@literal null}
* @param countQueryString
* @param sourceQuery the original source query, must not be {@literal null} or empty.
* @param queryRewriter must not be {@literal null}
* @param evaluationContextProvider must not be {@literal null}
* @param parser must not be {@literal null}
*/
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, @Nullable String countQueryString,
QueryRewriter queryRewriter, QueryMethodEvaluationContextProvider evaluationContextProvider, SpelExpressionParser parser) {
this(method, em, method.getRequiredAnnotatedQuery(), countQueryString, queryRewriter, evaluationContextProvider, parser);
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, @Nullable String sourceQuery,
QueryRewriter queryRewriter, QueryMethodEvaluationContextProvider evaluationContextProvider,
SpelExpressionParser parser) {
this(method, em, method.getRequiredAnnotatedQuery(), sourceQuery, queryRewriter, evaluationContextProvider,
parser);
}

/**
* Creates a new {@link SimpleJpaQuery} that encapsulates a simple query string.
*
* @param method must not be {@literal null}
* @param em must not be {@literal null}
* @param queryString must not be {@literal null} or empty
* @param sourceQuery the original source query, must not be {@literal null} or empty
* @param countQueryString
* @param queryRewriter
* @param evaluationContextProvider must not be {@literal null}
* @param parser must not be {@literal null}
*/
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String queryString, @Nullable String countQueryString, QueryRewriter queryRewriter,
QueryMethodEvaluationContextProvider evaluationContextProvider, SpelExpressionParser parser) {
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String sourceQuery, @Nullable String countQueryString,
QueryRewriter queryRewriter, QueryMethodEvaluationContextProvider evaluationContextProvider,
SpelExpressionParser parser) {

super(method, em, queryString, countQueryString, queryRewriter, evaluationContextProvider, parser);
super(method, em, sourceQuery, countQueryString, queryRewriter, evaluationContextProvider, parser);

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

if (method.isPageQuery()) {
validateQuery(getCountQuery().getQueryString(),
String.format("Count query validation failed for method %s", method));
validateQuery(getCountQuery(), "Count query %s validation failed for method %s", method);
}
}

Expand All @@ -81,23 +83,24 @@ public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String queryStrin
* @param query
* @param errorMessage
*/
private void validateQuery(String query, String errorMessage, Object... arguments) {
private void validateQuery(DeclaredQuery query, String errorMessage, JpaQueryMethod method) {

if (getQueryMethod().isProcedureQuery()) {
return;
}

EntityManager validatingEm = null;
var queryString = query.getQueryString();

try {
validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager();
validatingEm.createQuery(query);
validatingEm.createQuery(queryString);

} catch (RuntimeException e) {

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

} finally {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -175,14 +174,15 @@ void doesNotValidateCountQueryIfNotPagingMethod() throws Exception {
}

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

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

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

assertThatIllegalArgumentException().isThrownBy(() -> createJpaQuery(method)).withMessageContaining("Count")
assertThatIllegalArgumentException() //
.isThrownBy(() -> createJpaQuery(method)) //
.withMessageContaining("Count") //
.withMessageContaining(method.getName());
}

Expand Down