DATAJPA-44 - Domain class lookup in QueryMethod falls back to the repository domain class for methods not returning a domain class.
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.data.repository.query;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.data.repository.support.RepositoryMetadata;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
@@ -54,8 +55,8 @@ public interface QueryLookupStrategy {
|
||||
* that can be executed afterwards.
|
||||
*
|
||||
* @param method
|
||||
* @param domainClass
|
||||
* @param metadata
|
||||
* @return
|
||||
*/
|
||||
RepositoryQuery resolveQuery(Method method, Class<?> domainClass);
|
||||
RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata);
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.support.EntityMetadata;
|
||||
import org.springframework.data.repository.support.RepositoryMetadata;
|
||||
import org.springframework.data.repository.util.ClassUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -42,6 +43,7 @@ public class QueryMethod {
|
||||
SINGLE_ENTITY, PAGING, COLLECTION, MODIFYING;
|
||||
}
|
||||
|
||||
private final RepositoryMetadata metadata;
|
||||
private final Method method;
|
||||
private final Parameters parameters;
|
||||
|
||||
@@ -52,7 +54,7 @@ public class QueryMethod {
|
||||
*
|
||||
* @param method must not be {@literal null}
|
||||
*/
|
||||
public QueryMethod(Method method) {
|
||||
public QueryMethod(Method method, RepositoryMetadata metadata) {
|
||||
|
||||
Assert.notNull(method, "Method must not be null!");
|
||||
|
||||
@@ -75,6 +77,7 @@ public class QueryMethod {
|
||||
|
||||
this.method = method;
|
||||
this.parameters = new Parameters(method);
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
|
||||
@@ -103,8 +106,12 @@ public class QueryMethod {
|
||||
|
||||
|
||||
protected Class<?> getDomainClass() {
|
||||
|
||||
Class<?> repositoryDomainClass = metadata.getDomainClass();
|
||||
Class<?> methodDomainClass = ClassUtils.getReturnedDomainClass(method);
|
||||
|
||||
return ClassUtils.getReturnedDomainClass(method);
|
||||
return repositoryDomainClass == null || repositoryDomainClass.isAssignableFrom(methodDomainClass) ? methodDomainClass
|
||||
: repositoryDomainClass;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -285,17 +285,15 @@ public abstract class RepositoryFactorySupport {
|
||||
|
||||
for (Method method : repositoryInformation.getQueryMethods()) {
|
||||
RepositoryQuery query =
|
||||
lookupStrategy.resolveQuery(method,
|
||||
repositoryInformation.getDomainClass());
|
||||
invokeListeners(query, repositoryInformation);
|
||||
lookupStrategy.resolveQuery(method,repositoryInformation);
|
||||
invokeListeners(query);
|
||||
queries.put(method, query);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private void invokeListeners(RepositoryQuery query,
|
||||
RepositoryInformation information) {
|
||||
private void invokeListeners(RepositoryQuery query) {
|
||||
|
||||
for (QueryCreationListener listener : queryPostProcessors) {
|
||||
Class<?> typeArgument =
|
||||
|
||||
@@ -95,7 +95,7 @@ public class RepositoryFactorySupportUnitTests {
|
||||
RepositoryQuery queryTwo = mock(RepositoryQuery.class);
|
||||
|
||||
QueryLookupStrategy strategy = mock(QueryLookupStrategy.class);
|
||||
when(strategy.resolveQuery(any(Method.class), any(Class.class)))
|
||||
when(strategy.resolveQuery(any(Method.class), any(RepositoryMetadata.class)))
|
||||
.thenReturn(queryOne, queryTwo);
|
||||
|
||||
return strategy;
|
||||
|
||||
Reference in New Issue
Block a user