Skip to content

Commit fa11454

Browse files
committed
Log a warning when a query method is annotated with a query and a query name.
We now log when a query method has an ambiguous declaration to clarify that the declared query is used. Closes #2319
1 parent a88b90a commit fa11454

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategy.java

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import javax.persistence.EntityManager;
2121

22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
2225
import org.springframework.data.jpa.repository.Query;
2326
import org.springframework.data.projection.ProjectionFactory;
2427
import org.springframework.data.repository.core.NamedQueries;
@@ -40,6 +43,8 @@
4043
*/
4144
public final class JpaQueryLookupStrategy {
4245

46+
private static final Logger LOG = LoggerFactory.getLogger(JpaQueryLookupStrategy.class);
47+
4348
/**
4449
* Private constructor to prevent instantiation.
4550
*/
@@ -145,6 +150,11 @@ protected RepositoryQuery resolveQuery(JpaQueryMethod method, EntityManager em,
145150

146151
RepositoryQuery query = JpaQueryFactory.INSTANCE.fromQueryAnnotation(method, em, evaluationContextProvider);
147152

153+
if (query != null && method.hasAnnotatedQueryName()) {
154+
LOG.warn(String.format(
155+
"Query method %s is annotated with both, a query and a query name. Using the declared query.", method));
156+
}
157+
148158
if (null != query) {
149159
return query;
150160
}

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java

+8
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ String getAnnotatedQuery() {
274274
return StringUtils.hasText(query) ? query : null;
275275
}
276276

277+
/**
278+
* @return {@code true} if this method is annotated with {@code @Query(name=…)}.
279+
*/
280+
boolean hasAnnotatedQueryName() {
281+
return StringUtils.hasText(getAnnotationValue("name", String.class));
282+
}
283+
277284
/**
278285
* Returns the required query string declared in a {@link Query} annotation or throws {@link IllegalStateException} if
279286
* neither the annotation found nor the attribute was specified.
@@ -442,4 +449,5 @@ StoredProcedureAttributes getProcedureAttributes() {
442449

443450
return storedProcedureAttributes;
444451
}
452+
445453
}

src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.data.repository.query.QueryLookupStrategy;
4747
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
4848
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
49+
import org.springframework.data.repository.query.RepositoryQuery;
4950

5051
/**
5152
* Unit tests for {@link JpaQueryLookupStrategy}.
@@ -110,12 +111,28 @@ void sholdThrowMorePreciseExceptionIfTryingToUsePaginationInNativeQueries() thro
110111
.withMessageContaining(method.toString());
111112
}
112113

114+
@Test // GH-2319
115+
void prefersDeclaredQuery() throws Exception {
116+
117+
QueryLookupStrategy strategy = JpaQueryLookupStrategy.create(em, queryMethodFactory, Key.CREATE_IF_NOT_FOUND,
118+
EVALUATION_CONTEXT_PROVIDER, EscapeCharacter.DEFAULT);
119+
Method method = UserRepository.class.getMethod("annotatedQueryWithQueryAndQueryName");
120+
RepositoryMetadata metadata = new DefaultRepositoryMetadata(UserRepository.class);
121+
122+
RepositoryQuery repositoryQuery = strategy.resolveQuery(method, metadata, projectionFactory, namedQueries);
123+
124+
assertThat(repositoryQuery).isInstanceOf(AbstractStringBasedJpaQuery.class);
125+
}
126+
113127
interface UserRepository extends Repository<User, Integer> {
114128

115129
@Query("something absurd")
116130
User findByFoo(String foo);
117131

118132
@Query(value = "select u.* from User u", nativeQuery = true)
119133
List<User> findByInvalidNativeQuery(String param, Sort sort);
134+
135+
@Query(value = "something absurd", name = "my-query-name")
136+
User annotatedQueryWithQueryAndQueryName();
120137
}
121138
}

0 commit comments

Comments
 (0)