Skip to content

Commit a9403b5

Browse files
committed
DATAMONGO-2296 - Polishing.
Use getCollectionName() in MongoTemplate.insert/save. Consistently use getCollectionName(Class) from ReactiveMongoTemplate and fluent API implementations. Original pull request: #768.
1 parent 5f6291e commit a9403b5

13 files changed

+57
-91
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/EntityOperations.java

-11
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,6 @@ public String determineCollectionName(@Nullable Class<?> entityClass) {
116116
return context.getRequiredPersistentEntity(entityClass).getCollection();
117117
}
118118

119-
/**
120-
* Returns the collection name to be used for the given entity.
121-
*
122-
* @param obj can be {@literal null}.
123-
* @return
124-
*/
125-
@Nullable
126-
public String determineEntityCollectionName(@Nullable Object obj) {
127-
return null == obj ? null : determineCollectionName(obj.getClass());
128-
}
129-
130119
public Query getByIdInQuery(Collection<?> entities) {
131120

132121
MultiValueMap<String, Object> byIds = new LinkedMultiValueMap<>();

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ public <T> T insert(T objectToSave) {
11601160
Assert.notNull(objectToSave, "ObjectToSave must not be null!");
11611161

11621162
ensureNotIterable(objectToSave);
1163-
return insert(objectToSave, operations.determineEntityCollectionName(objectToSave));
1163+
return insert(objectToSave, getCollectionName(ClassUtils.getUserClass(objectToSave)));
11641164
}
11651165

11661166
/*
@@ -1289,9 +1289,7 @@ protected <T> Collection<T> doInsertAll(Collection<? extends T> listToSave, Mong
12891289
continue;
12901290
}
12911291

1292-
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(element.getClass());
1293-
1294-
String collection = entity.getCollection();
1292+
String collection = getCollectionName(ClassUtils.getUserClass(element));
12951293
List<T> collectionElements = elementsByCollection.get(collection);
12961294

12971295
if (null == collectionElements) {
@@ -1355,7 +1353,7 @@ protected <T> Collection<T> doInsertBatch(String collectionName, Collection<? ex
13551353
public <T> T save(T objectToSave) {
13561354

13571355
Assert.notNull(objectToSave, "Object to save must not be null!");
1358-
return save(objectToSave, operations.determineEntityCollectionName(objectToSave));
1356+
return save(objectToSave, getCollectionName(ClassUtils.getUserClass(objectToSave)));
13591357
}
13601358

13611359
@Override

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveAggregationOperationSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ private String getCollectionName(Aggregation aggregation) {
116116
TypedAggregation<?> typedAggregation = (TypedAggregation<?>) aggregation;
117117

118118
if (typedAggregation.getInputType() != null) {
119-
return template.determineCollectionName(typedAggregation.getInputType());
119+
return template.getCollectionName(typedAggregation.getInputType());
120120
}
121121
}
122122

123-
return template.determineCollectionName(domainType);
123+
return template.getCollectionName(domainType);
124124
}
125125
}
126126
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private FindPublisherPreparer getCursorPreparer(Query query) {
238238
}
239239

240240
private String getCollectionName() {
241-
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
241+
return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
242242
}
243243

244244
private String asString() {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveInsertOperationSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public ReactiveInsert<T> inCollection(String collection) {
9696
}
9797

9898
private String getCollectionName() {
99-
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
99+
return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
100100
}
101101
}
102102
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMapReduceOperationSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public ReactiveMapReduce<T> reduce(String reduceFunction) {
171171
}
172172

173173
private String getCollectionName() {
174-
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
174+
return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
175175
}
176176
}
177177
}

0 commit comments

Comments
 (0)