Distinguish between getDomainClass() and getReturnedDomainClass().
In a repository for a particular domain type we might want to query for nested documents directly and thus also return those nested documents. As the collection to be queried is determined by the domain class, this one might differ from the object the result has to be unmarshalled to.
This commit is contained in:
@@ -42,7 +42,7 @@ import com.mongodb.DBObject;
|
||||
*/
|
||||
public abstract class AbstractMongoQuery implements RepositoryQuery {
|
||||
|
||||
private final QueryMethod method;
|
||||
private final MongoQueryMethod method;
|
||||
private final MongoTemplate template;
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
|
||||
* @param method
|
||||
* @param template
|
||||
*/
|
||||
public AbstractMongoQuery(QueryMethod method, MongoTemplate template) {
|
||||
public AbstractMongoQuery(MongoQueryMethod method, MongoTemplate template) {
|
||||
|
||||
Assert.notNull(template);
|
||||
Assert.notNull(method);
|
||||
@@ -103,7 +103,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
|
||||
|
||||
String collectionName = getCollectionName(method.getDomainClass());
|
||||
return template
|
||||
.find(collectionName, query, method.getDomainClass());
|
||||
.find(collectionName, query, method.getReturnedDomainClass());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
|
||||
|
||||
List<?> result =
|
||||
template.find(collectionName, applyPagination(query, pageable),
|
||||
method.getDomainClass());
|
||||
method.getReturnedDomainClass());
|
||||
|
||||
return new PageImpl(result, pageable, count);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
import org.springframework.data.repository.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -30,17 +31,43 @@ import org.springframework.util.StringUtils;
|
||||
class MongoQueryMethod extends QueryMethod {
|
||||
|
||||
private final Method method;
|
||||
private final Class<?> domainClass;
|
||||
|
||||
/**
|
||||
* Creates a new {@link MongoQueryMethod} from the given {@link Method}.
|
||||
*
|
||||
* @param method
|
||||
*/
|
||||
public MongoQueryMethod(Method method) {
|
||||
public MongoQueryMethod(Method method, Class<?> domainClass) {
|
||||
super(method);
|
||||
this.method = method;
|
||||
this.domainClass = domainClass;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.query.QueryMethod#getDomainClass()
|
||||
*/
|
||||
@Override
|
||||
public Class<?> getDomainClass() {
|
||||
return this.domainClass;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the type that will be returned by the query method.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Class<?> getReturnedDomainClass() {
|
||||
return ClassUtils.getReturnedDomainClass(method);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether the method has an annotated query.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
boolean hasAnnotatedQuery() {
|
||||
return getAnnotatedQuery() != null;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ public class PartTreeMongoQuery extends AbstractMongoQuery {
|
||||
* @param method
|
||||
* @param template
|
||||
*/
|
||||
public PartTreeMongoQuery(QueryMethod method, MongoTemplate template) {
|
||||
public PartTreeMongoQuery(MongoQueryMethod method, MongoTemplate template) {
|
||||
|
||||
super(method, template);
|
||||
this.tree = new PartTree(method.getName(), method.getDomainClass());
|
||||
|
||||
Reference in New Issue
Block a user