From 851acca320b5e7eb7c501b9f7122cbac11d00f4f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 1 Mar 2011 21:11:31 +0100 Subject: [PATCH] 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. --- .../repository/AbstractMongoQuery.java | 8 ++--- .../mongodb/repository/MongoQueryMethod.java | 29 ++++++++++++++++++- .../repository/PartTreeMongoQuery.java | 2 +- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java index c6b72d0c1..f312d3049 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java @@ -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); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java index ec921de1b..8b742abce 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java @@ -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; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/PartTreeMongoQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/PartTreeMongoQuery.java index b6221b8e9..a9914874a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/PartTreeMongoQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/PartTreeMongoQuery.java @@ -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());