Browse Source

DATAMONGO-1043 - Make sure we dynamically lookup SpEL based collection names for query execution.

Changed SimpleMongoEntityMetadata to keep a reference to the collection entity instead of the eagerly resolved collection name. This is to make sure the name gets re-evaluated for every query execution to support dynamically changing collections defined via SpEL expressions.

Related pull request: #238.
pull/236/head
Oliver Gierke 11 years ago
parent
commit
cbbafce73d
  1. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java
  2. 16
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/SimpleMongoEntityMetadata.java

3
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java

@ -128,8 +128,7 @@ public class MongoQueryMethod extends QueryMethod {
MongoPersistentEntity<?> collectionEntity = domainClass.isAssignableFrom(returnedObjectType) ? returnedEntity MongoPersistentEntity<?> collectionEntity = domainClass.isAssignableFrom(returnedObjectType) ? returnedEntity
: managedEntity; : managedEntity;
this.metadata = new SimpleMongoEntityMetadata<Object>((Class<Object>) returnedEntity.getType(), this.metadata = new SimpleMongoEntityMetadata<Object>((Class<Object>) returnedEntity.getType(), collectionEntity);
collectionEntity.getCollection());
} }
return this.metadata; return this.metadata;

16
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/SimpleMongoEntityMetadata.java

@ -15,6 +15,7 @@
*/ */
package org.springframework.data.mongodb.repository.query; package org.springframework.data.mongodb.repository.query;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -25,21 +26,22 @@ import org.springframework.util.Assert;
class SimpleMongoEntityMetadata<T> implements MongoEntityMetadata<T> { class SimpleMongoEntityMetadata<T> implements MongoEntityMetadata<T> {
private final Class<T> type; private final Class<T> type;
private final String collectionName; private final MongoPersistentEntity<?> collectionEntity;
/** /**
* Creates a new {@link SimpleMongoEntityMetadata} using the given type and collection name. * Creates a new {@link SimpleMongoEntityMetadata} using the given type and {@link MongoPersistentEntity} to use for
* collection lookups.
* *
* @param type must not be {@literal null}. * @param type must not be {@literal null}.
* @param collectionName must not be {@literal null} or empty. * @param collectionEntity must not be {@literal null} or empty.
*/ */
public SimpleMongoEntityMetadata(Class<T> type, String collectionName) { public SimpleMongoEntityMetadata(Class<T> type, MongoPersistentEntity<?> collectionEntity) {
Assert.notNull(type, "Type must not be null!"); Assert.notNull(type, "Type must not be null!");
Assert.hasText(collectionName, "Collection name must not be null or empty!"); Assert.notNull(collectionEntity, "Collection entity must not be null or empty!");
this.type = type; this.type = type;
this.collectionName = collectionName; this.collectionEntity = collectionEntity;
} }
/* /*
@ -55,6 +57,6 @@ class SimpleMongoEntityMetadata<T> implements MongoEntityMetadata<T> {
* @see org.springframework.data.mongodb.repository.query.MongoEntityMetadata#getCollectionName() * @see org.springframework.data.mongodb.repository.query.MongoEntityMetadata#getCollectionName()
*/ */
public String getCollectionName() { public String getCollectionName() {
return collectionName; return collectionEntity.getCollection();
} }
} }

Loading…
Cancel
Save