Skip to content

Commit

Permalink
Refine NamedQuery error messages when using Sort/Pageable parameters.
Browse files Browse the repository at this point in the history
Closes #3660
  • Loading branch information
mp911de committed Dec 9, 2024
1 parent eb9f746 commit e4a2416
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -70,20 +69,10 @@ public class JpaQueryMethod extends QueryMethod {
* Persistence Specification: Persistent Fields and Properties - Paragraph starting with
* "Collection-valued persistent...".</a>
*/
private static final Set<Class<?>> NATIVE_ARRAY_TYPES;
private static final Set<Class<?>> NATIVE_ARRAY_TYPES = Set.of(byte[].class, Byte[].class, char[].class,
Character[].class);
private static final StoredProcedureAttributeSource storedProcedureAttributeSource = StoredProcedureAttributeSource.INSTANCE;

static {

Set<Class<?>> types = new HashSet<>();
types.add(byte[].class);
types.add(Byte[].class);
types.add(char[].class);
types.add(Character[].class);

NATIVE_ARRAY_TYPES = Collections.unmodifiableSet(types);
}

private final QueryExtractor extractor;
private final Method method;
private final Class<?> returnType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ private NamedQuery(JpaQueryMethod method, EntityManager em) {
Parameters<?, ?> parameters = method.getParameters();

if (parameters.hasSortParameter()) {
throw new IllegalStateException(String.format("Finder method %s is backed by a NamedQuery and must "
+ "not contain a sort parameter as we cannot modify the query; Use @Query instead", method));
throw new IllegalStateException(String.format("Query method %s is backed by a NamedQuery and must "
+ "not contain a sort parameter as we cannot modify the query; Use @%s(value=…) instead to apply sorting or remove the 'Sort' parameter.",
method, method.isNativeQuery() ? "NativeQuery" : "Query"));
}

this.namedCountQueryIsPresent = hasNamedQuery(em, countQueryName);
Expand All @@ -85,14 +86,14 @@ private NamedQuery(JpaQueryMethod method, EntityManager em) {

if (parameters.hasPageableParameter()) {
LOG.warn(String.format(
"Finder method %s is backed by a NamedQuery but contains a Pageable parameter; Sorting delivered via this Pageable will not be applied",
method));
"Query method %s is backed by a NamedQuery but contains a Pageable parameter; Sorting delivered via this Pageable will not be applied; Use @%s(value=…) instead to apply sorting.",
method, method.isNativeQuery() ? "NativeQuery" : "Query"));
}

String queryString = extractor.extractQueryString(query);

// TODO: Detect whether a named query is a native one.
this.declaredQuery = Lazy.of(() -> DeclaredQuery.of(queryString, query.toString().contains("NativeQuery")));
this.declaredQuery = Lazy
.of(() -> DeclaredQuery.of(queryString, method.isNativeQuery() || query.toString().contains("NativeQuery")));
this.metadataCache = new QueryParameterSetter.QueryMetadataCache();
}

Expand Down Expand Up @@ -133,20 +134,22 @@ public static RepositoryQuery lookupFrom(JpaQueryMethod method, EntityManager em
String queryName = method.getNamedQueryName();

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Looking up named query %s", queryName));
LOG.debug(String.format("Looking up named query '%s'", queryName));
}

if (!hasNamedQuery(em, queryName)) {
return null;
}

if (method.isScrollQuery()) {
throw QueryCreationException.create(method, "Scroll queries are not supported using String-based queries");
throw QueryCreationException.create(method, String.format(
"Scroll queries are not supported using String-based queries as we cannot rewrite the query string. Use @%s(value=…) instead.",
method.isNativeQuery() ? "NativeQuery" : "Query"));
}

RepositoryQuery query = new NamedQuery(method, em);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Found named query %s", queryName));
LOG.debug(String.format("Found named query '%s'", queryName));
}
return query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void namedQueryWithSortShouldThrowIllegalStateException() throws NoSuchMethodExc
assertThatIllegalStateException()
.isThrownBy(() -> strategy.resolveQuery(method, metadata, projectionFactory, namedQueries))
.withMessageContaining(
"is backed by a NamedQuery and must not contain a sort parameter as we cannot modify the query; Use @Query instead");
"is backed by a NamedQuery and must not contain a sort parameter as we cannot modify the query; Use @Query(value=…) instead to apply sorting or remove the 'Sort' parameter.");
}

@Test // GH-2018
Expand Down

0 comments on commit e4a2416

Please sign in to comment.