Skip to content

Commit cbbafce

Browse files
committed
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.
1 parent 2e74c19 commit cbbafce

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ public MongoEntityMetadata<?> getEntityInformation() {
128128
MongoPersistentEntity<?> collectionEntity = domainClass.isAssignableFrom(returnedObjectType) ? returnedEntity
129129
: managedEntity;
130130

131-
this.metadata = new SimpleMongoEntityMetadata<Object>((Class<Object>) returnedEntity.getType(),
132-
collectionEntity.getCollection());
131+
this.metadata = new SimpleMongoEntityMetadata<Object>((Class<Object>) returnedEntity.getType(), collectionEntity);
133132
}
134133

135134
return this.metadata;

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.query;
1717

18+
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
1819
import org.springframework.util.Assert;
1920

2021
/**
@@ -25,21 +26,22 @@
2526
class SimpleMongoEntityMetadata<T> implements MongoEntityMetadata<T> {
2627

2728
private final Class<T> type;
28-
private final String collectionName;
29+
private final MongoPersistentEntity<?> collectionEntity;
2930

3031
/**
31-
* Creates a new {@link SimpleMongoEntityMetadata} using the given type and collection name.
32+
* Creates a new {@link SimpleMongoEntityMetadata} using the given type and {@link MongoPersistentEntity} to use for
33+
* collection lookups.
3234
*
3335
* @param type must not be {@literal null}.
34-
* @param collectionName must not be {@literal null} or empty.
36+
* @param collectionEntity must not be {@literal null} or empty.
3537
*/
36-
public SimpleMongoEntityMetadata(Class<T> type, String collectionName) {
38+
public SimpleMongoEntityMetadata(Class<T> type, MongoPersistentEntity<?> collectionEntity) {
3739

3840
Assert.notNull(type, "Type must not be null!");
39-
Assert.hasText(collectionName, "Collection name must not be null or empty!");
41+
Assert.notNull(collectionEntity, "Collection entity must not be null or empty!");
4042

4143
this.type = type;
42-
this.collectionName = collectionName;
44+
this.collectionEntity = collectionEntity;
4345
}
4446

4547
/*
@@ -55,6 +57,6 @@ public Class<T> getJavaType() {
5557
* @see org.springframework.data.mongodb.repository.query.MongoEntityMetadata#getCollectionName()
5658
*/
5759
public String getCollectionName() {
58-
return collectionName;
60+
return collectionEntity.getCollection();
5961
}
6062
}

0 commit comments

Comments
 (0)