DATAMONGO-2296 - Polishing.

Use getCollectionName() in MongoTemplate.insert/save. Consistently use getCollectionName(Class) from ReactiveMongoTemplate and fluent API implementations.

Original pull request: #768.
This commit is contained in:
Mark Paluch
2019-07-01 16:35:48 +02:00
parent 5f6291ed32
commit a9403b526f
13 changed files with 57 additions and 91 deletions

View File

@@ -116,17 +116,6 @@ class EntityOperations {
return context.getRequiredPersistentEntity(entityClass).getCollection(); return context.getRequiredPersistentEntity(entityClass).getCollection();
} }
/**
* Returns the collection name to be used for the given entity.
*
* @param obj can be {@literal null}.
* @return
*/
@Nullable
public String determineEntityCollectionName(@Nullable Object obj) {
return null == obj ? null : determineCollectionName(obj.getClass());
}
public Query getByIdInQuery(Collection<?> entities) { public Query getByIdInQuery(Collection<?> entities) {
MultiValueMap<String, Object> byIds = new LinkedMultiValueMap<>(); MultiValueMap<String, Object> byIds = new LinkedMultiValueMap<>();

View File

@@ -1160,7 +1160,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
Assert.notNull(objectToSave, "ObjectToSave must not be null!"); Assert.notNull(objectToSave, "ObjectToSave must not be null!");
ensureNotIterable(objectToSave); ensureNotIterable(objectToSave);
return insert(objectToSave, operations.determineEntityCollectionName(objectToSave)); return insert(objectToSave, getCollectionName(ClassUtils.getUserClass(objectToSave)));
} }
/* /*
@@ -1289,9 +1289,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
continue; continue;
} }
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(element.getClass()); String collection = getCollectionName(ClassUtils.getUserClass(element));
String collection = entity.getCollection();
List<T> collectionElements = elementsByCollection.get(collection); List<T> collectionElements = elementsByCollection.get(collection);
if (null == collectionElements) { if (null == collectionElements) {
@@ -1355,7 +1353,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
public <T> T save(T objectToSave) { public <T> T save(T objectToSave) {
Assert.notNull(objectToSave, "Object to save must not be null!"); Assert.notNull(objectToSave, "Object to save must not be null!");
return save(objectToSave, operations.determineEntityCollectionName(objectToSave)); return save(objectToSave, getCollectionName(ClassUtils.getUserClass(objectToSave)));
} }
@Override @Override

View File

@@ -116,11 +116,11 @@ class ReactiveAggregationOperationSupport implements ReactiveAggregationOperatio
TypedAggregation<?> typedAggregation = (TypedAggregation<?>) aggregation; TypedAggregation<?> typedAggregation = (TypedAggregation<?>) aggregation;
if (typedAggregation.getInputType() != null) { if (typedAggregation.getInputType() != null) {
return template.determineCollectionName(typedAggregation.getInputType()); return template.getCollectionName(typedAggregation.getInputType());
} }
} }
return template.determineCollectionName(domainType); return template.getCollectionName(domainType);
} }
} }
} }

View File

@@ -238,7 +238,7 @@ class ReactiveFindOperationSupport implements ReactiveFindOperation {
} }
private String getCollectionName() { private String getCollectionName() {
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType); return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
} }
private String asString() { private String asString() {

View File

@@ -96,7 +96,7 @@ class ReactiveInsertOperationSupport implements ReactiveInsertOperation {
} }
private String getCollectionName() { private String getCollectionName() {
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType); return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
} }
} }
} }

View File

@@ -171,7 +171,7 @@ class ReactiveMapReduceOperationSupport implements ReactiveMapReduceOperation {
} }
private String getCollectionName() { private String getCollectionName() {
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType); return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
} }
} }
} }

View File

@@ -375,12 +375,12 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#reactiveIndexOps(java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#reactiveIndexOps(java.lang.Class)
*/ */
public ReactiveIndexOperations indexOps(Class<?> entityClass) { public ReactiveIndexOperations indexOps(Class<?> entityClass) {
return new DefaultReactiveIndexOperations(this, determineCollectionName(entityClass), this.queryMapper, return new DefaultReactiveIndexOperations(this, getCollectionName(entityClass), this.queryMapper,
entityClass); entityClass);
} }
public String getCollectionName(Class<?> entityClass) { public String getCollectionName(Class<?> entityClass) {
return this.determineCollectionName(entityClass); return operations.determineCollectionName(entityClass);
} }
/* /*
@@ -420,7 +420,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
*/ */
@Override @Override
public <T> Flux<T> execute(Class<?> entityClass, ReactiveCollectionCallback<T> action) { public <T> Flux<T> execute(Class<?> entityClass, ReactiveCollectionCallback<T> action) {
return createFlux(determineCollectionName(entityClass), action); return createFlux(getCollectionName(entityClass), action);
} }
/* /*
@@ -606,7 +606,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#createCollection(java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#createCollection(java.lang.Class)
*/ */
public <T> Mono<MongoCollection<Document>> createCollection(Class<T> entityClass) { public <T> Mono<MongoCollection<Document>> createCollection(Class<T> entityClass) {
return createCollection(determineCollectionName(entityClass)); return createCollection(getCollectionName(entityClass));
} }
/* /*
@@ -615,7 +615,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
*/ */
public <T> Mono<MongoCollection<Document>> createCollection(Class<T> entityClass, public <T> Mono<MongoCollection<Document>> createCollection(Class<T> entityClass,
@Nullable CollectionOptions collectionOptions) { @Nullable CollectionOptions collectionOptions) {
return doCreateCollection(determineCollectionName(entityClass), return doCreateCollection(getCollectionName(entityClass),
convertToCreateCollectionOptions(collectionOptions, entityClass)); convertToCreateCollectionOptions(collectionOptions, entityClass));
} }
@@ -649,7 +649,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#collectionExists(java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#collectionExists(java.lang.Class)
*/ */
public <T> Mono<Boolean> collectionExists(Class<T> entityClass) { public <T> Mono<Boolean> collectionExists(Class<T> entityClass) {
return collectionExists(determineCollectionName(entityClass)); return collectionExists(getCollectionName(entityClass));
} }
/* /*
@@ -668,7 +668,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#dropCollection(java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#dropCollection(java.lang.Class)
*/ */
public <T> Mono<Void> dropCollection(Class<T> entityClass) { public <T> Mono<Void> dropCollection(Class<T> entityClass) {
return dropCollection(determineCollectionName(entityClass)); return dropCollection(getCollectionName(entityClass));
} }
/* /*
@@ -705,7 +705,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findOne(org.springframework.data.mongodb.core.query.Query, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findOne(org.springframework.data.mongodb.core.query.Query, java.lang.Class)
*/ */
public <T> Mono<T> findOne(Query query, Class<T> entityClass) { public <T> Mono<T> findOne(Query query, Class<T> entityClass) {
return findOne(query, entityClass, determineCollectionName(entityClass)); return findOne(query, entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -728,7 +728,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#exists(org.springframework.data.mongodb.core.query.Query, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#exists(org.springframework.data.mongodb.core.query.Query, java.lang.Class)
*/ */
public Mono<Boolean> exists(Query query, Class<?> entityClass) { public Mono<Boolean> exists(Query query, Class<?> entityClass) {
return exists(query, entityClass, determineCollectionName(entityClass)); return exists(query, entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -771,7 +771,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#find(org.springframework.data.mongodb.core.query.Query, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#find(org.springframework.data.mongodb.core.query.Query, java.lang.Class)
*/ */
public <T> Flux<T> find(Query query, Class<T> entityClass) { public <T> Flux<T> find(Query query, Class<T> entityClass) {
return find(query, entityClass, determineCollectionName(entityClass)); return find(query, entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -793,7 +793,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findById(java.lang.Object, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findById(java.lang.Object, java.lang.Class)
*/ */
public <T> Mono<T> findById(Object id, Class<T> entityClass) { public <T> Mono<T> findById(Object id, Class<T> entityClass) {
return findById(id, entityClass, determineCollectionName(entityClass)); return findById(id, entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -812,7 +812,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findDistinct(org.springframework.data.mongodb.core.query.Query, java.lang.String, java.lang.Class, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findDistinct(org.springframework.data.mongodb.core.query.Query, java.lang.String, java.lang.Class, java.lang.Class)
*/ */
public <T> Flux<T> findDistinct(Query query, String field, Class<?> entityClass, Class<T> resultClass) { public <T> Flux<T> findDistinct(Query query, String field, Class<?> entityClass, Class<T> resultClass) {
return findDistinct(query, field, determineCollectionName(entityClass), entityClass, resultClass); return findDistinct(query, field, getCollectionName(entityClass), entityClass, resultClass);
} }
/* /*
@@ -907,7 +907,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
*/ */
@Override @Override
public <O> Flux<O> aggregate(TypedAggregation<?> aggregation, Class<O> outputType) { public <O> Flux<O> aggregate(TypedAggregation<?> aggregation, Class<O> outputType) {
return aggregate(aggregation, determineCollectionName(aggregation.getInputType()), outputType); return aggregate(aggregation, getCollectionName(aggregation.getInputType()), outputType);
} }
/* /*
@@ -917,7 +917,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
@Override @Override
public <O> Flux<O> aggregate(Aggregation aggregation, Class<?> inputType, Class<O> outputType) { public <O> Flux<O> aggregate(Aggregation aggregation, Class<?> inputType, Class<O> outputType) {
return aggregate(aggregation, determineCollectionName(inputType), outputType, return aggregate(aggregation, getCollectionName(inputType), outputType,
new TypeBasedAggregationOperationContext(inputType, mappingContext, queryMapper)); new TypeBasedAggregationOperationContext(inputType, mappingContext, queryMapper));
} }
@@ -983,7 +983,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
*/ */
@Override @Override
public <T> Flux<GeoResult<T>> geoNear(NearQuery near, Class<T> entityClass) { public <T> Flux<GeoResult<T>> geoNear(NearQuery near, Class<T> entityClass) {
return geoNear(near, entityClass, determineCollectionName(entityClass)); return geoNear(near, entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -1007,7 +1007,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
throw new InvalidDataAccessApiUsageException("Entity class must not be null!"); throw new InvalidDataAccessApiUsageException("Entity class must not be null!");
} }
String collection = StringUtils.hasText(collectionName) ? collectionName : determineCollectionName(entityClass); String collection = StringUtils.hasText(collectionName) ? collectionName : getCollectionName(entityClass);
Document nearDocument = near.toDocument(); Document nearDocument = near.toDocument();
Document command = new Document("geoNear", collection); Document command = new Document("geoNear", collection);
@@ -1043,7 +1043,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findAndModify(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findAndModify(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.Class)
*/ */
public <T> Mono<T> findAndModify(Query query, Update update, Class<T> entityClass) { public <T> Mono<T> findAndModify(Query query, Update update, Class<T> entityClass) {
return findAndModify(query, update, new FindAndModifyOptions(), entityClass, determineCollectionName(entityClass)); return findAndModify(query, update, new FindAndModifyOptions(), entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -1059,7 +1059,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findAndModify(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, org.springframework.data.mongodb.core.FindAndModifyOptions, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findAndModify(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, org.springframework.data.mongodb.core.FindAndModifyOptions, java.lang.Class)
*/ */
public <T> Mono<T> findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass) { public <T> Mono<T> findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass) {
return findAndModify(query, update, options, entityClass, determineCollectionName(entityClass)); return findAndModify(query, update, options, entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -1118,7 +1118,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findAndRemove(org.springframework.data.mongodb.core.query.Query, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findAndRemove(org.springframework.data.mongodb.core.query.Query, java.lang.Class)
*/ */
public <T> Mono<T> findAndRemove(Query query, Class<T> entityClass) { public <T> Mono<T> findAndRemove(Query query, Class<T> entityClass) {
return findAndRemove(query, entityClass, determineCollectionName(entityClass)); return findAndRemove(query, entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -1139,7 +1139,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
Assert.notNull(entityClass, "Entity class must not be null!"); Assert.notNull(entityClass, "Entity class must not be null!");
return count(query, entityClass, determineCollectionName(entityClass)); return count(query, entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -1196,7 +1196,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
*/ */
@Override @Override
public <T> Flux<T> insertAll(Mono<? extends Collection<? extends T>> batchToSave, Class<?> entityClass) { public <T> Flux<T> insertAll(Mono<? extends Collection<? extends T>> batchToSave, Class<?> entityClass) {
return insertAll(batchToSave, determineCollectionName(entityClass)); return insertAll(batchToSave, getCollectionName(entityClass));
} }
/* /*
@@ -1220,7 +1220,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
Assert.notNull(objectToSave, "Object to insert must not be null!"); Assert.notNull(objectToSave, "Object to insert must not be null!");
ensureNotIterable(objectToSave); ensureNotIterable(objectToSave);
return insert(objectToSave, determineEntityCollectionName(objectToSave)); return insert(objectToSave, getCollectionName(ClassUtils.getUserClass(objectToSave)));
} }
/* /*
@@ -1266,7 +1266,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#insert(java.util.Collection, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#insert(java.util.Collection, java.lang.Class)
*/ */
public <T> Flux<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass) { public <T> Flux<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass) {
return doInsertBatch(determineCollectionName(entityClass), batchToSave, this.mongoConverter); return doInsertBatch(getCollectionName(entityClass), batchToSave, this.mongoConverter);
} }
/* /*
@@ -1300,9 +1300,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
listToSave.forEach(element -> { listToSave.forEach(element -> {
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(element.getClass()); String collection = getCollectionName(element.getClass());
String collection = entity.getCollection();
List<T> collectionElements = elementsByCollection.computeIfAbsent(collection, k -> new ArrayList<>()); List<T> collectionElements = elementsByCollection.computeIfAbsent(collection, k -> new ArrayList<>());
collectionElements.add(element); collectionElements.add(element);
@@ -1382,7 +1380,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
public <T> Mono<T> save(T objectToSave) { public <T> Mono<T> save(T objectToSave) {
Assert.notNull(objectToSave, "Object to save must not be null!"); Assert.notNull(objectToSave, "Object to save must not be null!");
return save(objectToSave, determineEntityCollectionName(objectToSave)); return save(objectToSave, getCollectionName(ClassUtils.getUserClass(objectToSave)));
} }
/* /*
@@ -1546,7 +1544,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#upsert(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#upsert(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.Class)
*/ */
public Mono<UpdateResult> upsert(Query query, Update update, Class<?> entityClass) { public Mono<UpdateResult> upsert(Query query, Update update, Class<?> entityClass) {
return doUpdate(determineCollectionName(entityClass), query, update, entityClass, true, false); return doUpdate(getCollectionName(entityClass), query, update, entityClass, true, false);
} }
/* /*
@@ -1570,7 +1568,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#updateFirst(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#updateFirst(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.Class)
*/ */
public Mono<UpdateResult> updateFirst(Query query, Update update, Class<?> entityClass) { public Mono<UpdateResult> updateFirst(Query query, Update update, Class<?> entityClass) {
return doUpdate(determineCollectionName(entityClass), query, update, entityClass, false, false); return doUpdate(getCollectionName(entityClass), query, update, entityClass, false, false);
} }
/* /*
@@ -1594,7 +1592,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#updateMulti(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#updateMulti(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.Class)
*/ */
public Mono<UpdateResult> updateMulti(Query query, Update update, Class<?> entityClass) { public Mono<UpdateResult> updateMulti(Query query, Update update, Class<?> entityClass) {
return doUpdate(determineCollectionName(entityClass), query, update, entityClass, false, true); return doUpdate(getCollectionName(entityClass), query, update, entityClass, false, true);
} }
/* /*
@@ -1770,7 +1768,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#remove(org.springframework.data.mongodb.core.query.Query, java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#remove(org.springframework.data.mongodb.core.query.Query, java.lang.Class)
*/ */
public Mono<DeleteResult> remove(Query query, Class<?> entityClass) { public Mono<DeleteResult> remove(Query query, Class<?> entityClass) {
return remove(query, entityClass, determineCollectionName(entityClass)); return remove(query, entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -1838,7 +1836,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findAll(java.lang.Class) * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findAll(java.lang.Class)
*/ */
public <T> Flux<T> findAll(Class<T> entityClass) { public <T> Flux<T> findAll(Class<T> entityClass) {
return findAll(entityClass, determineCollectionName(entityClass)); return findAll(entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -1866,7 +1864,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
*/ */
@Override @Override
public <T> Flux<T> findAllAndRemove(Query query, Class<T> entityClass) { public <T> Flux<T> findAllAndRemove(Query query, Class<T> entityClass) {
return findAllAndRemove(query, entityClass, determineCollectionName(entityClass)); return findAllAndRemove(query, entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -1884,7 +1882,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
*/ */
@Override @Override
public <T> Flux<T> tail(Query query, Class<T> entityClass) { public <T> Flux<T> tail(Query query, Class<T> entityClass) {
return tail(query, entityClass, determineCollectionName(entityClass)); return tail(query, entityClass, getCollectionName(entityClass));
} }
/* /*
@@ -1967,7 +1965,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
public <T> Flux<T> mapReduce(Query filterQuery, Class<?> domainType, Class<T> resultType, String mapFunction, public <T> Flux<T> mapReduce(Query filterQuery, Class<?> domainType, Class<T> resultType, String mapFunction,
String reduceFunction, MapReduceOptions options) { String reduceFunction, MapReduceOptions options) {
return mapReduce(filterQuery, domainType, determineCollectionName(domainType), resultType, mapFunction, return mapReduce(filterQuery, domainType, getCollectionName(domainType), resultType, mapFunction,
reduceFunction, options); reduceFunction, options);
} }
@@ -2595,25 +2593,6 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
return type == null ? null : mappingContext.getPersistentEntity(type); return type == null ? null : mappingContext.getPersistentEntity(type);
} }
private <T> String determineEntityCollectionName(@Nullable T obj) {
if (null != obj) {
return determineCollectionName(obj.getClass());
}
return null;
}
String determineCollectionName(@Nullable Class<?> entityClass) {
if (entityClass == null) {
throw new InvalidDataAccessApiUsageException(
"No class parameter provided, entity collection can't be determined!");
}
return mappingContext.getRequiredPersistentEntity(entityClass).getCollection();
}
private static MappingMongoConverter getDefaultMongoConverter() { private static MappingMongoConverter getDefaultMongoConverter() {
MongoCustomConversions conversions = new MongoCustomConversions(Collections.emptyList()); MongoCustomConversions conversions = new MongoCustomConversions(Collections.emptyList());

View File

@@ -112,7 +112,7 @@ class ReactiveRemoveOperationSupport implements ReactiveRemoveOperation {
} }
private String getCollectionName() { private String getCollectionName() {
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType); return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
} }
} }

View File

@@ -126,7 +126,7 @@ class ReactiveUpdateOperationSupport implements ReactiveUpdateOperation {
return template.findAndModify(query, update, findAndModifyOptions, targetType, collectionName); return template.findAndModify(query, update, findAndModifyOptions, targetType, collectionName);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.ReactiveUpdateOperation.TerminatingFindAndReplace#findAndReplace() * @see org.springframework.data.mongodb.core.ReactiveUpdateOperation.TerminatingFindAndReplace#findAndReplace()
*/ */
@@ -172,7 +172,7 @@ class ReactiveUpdateOperationSupport implements ReactiveUpdateOperation {
findAndReplaceOptions, replacement, targetType); findAndReplaceOptions, replacement, targetType);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.ReactiveUpdateOperation.UpdateWithUpdate#replaceWith(java.lang.Object) * @see org.springframework.data.mongodb.core.ReactiveUpdateOperation.UpdateWithUpdate#replaceWith(java.lang.Object)
*/ */
@@ -185,7 +185,7 @@ class ReactiveUpdateOperationSupport implements ReactiveUpdateOperation {
findAndReplaceOptions, replacement, targetType); findAndReplaceOptions, replacement, targetType);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.ReactiveUpdateOperation.FindAndReplaceWithOptions#withOptions(org.springframework.data.mongodb.core.FindAndReplaceOptions) * @see org.springframework.data.mongodb.core.ReactiveUpdateOperation.FindAndReplaceWithOptions#withOptions(org.springframework.data.mongodb.core.FindAndReplaceOptions)
*/ */
@@ -216,7 +216,7 @@ class ReactiveUpdateOperationSupport implements ReactiveUpdateOperation {
} }
private String getCollectionName() { private String getCollectionName() {
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType); return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
} }
} }
} }

View File

@@ -78,13 +78,13 @@ public class ReactiveAggregationOperationSupportUnitTests {
@Test // DATAMONGO-1719 @Test // DATAMONGO-1719
public void aggregateWithUntypedAggregation() { public void aggregateWithUntypedAggregation() {
when(template.determineCollectionName(any(Class.class))).thenReturn("person"); when(template.getCollectionName(any(Class.class))).thenReturn("person");
opSupport.aggregateAndReturn(Person.class).by(newAggregation(project("foo"))).all(); opSupport.aggregateAndReturn(Person.class).by(newAggregation(project("foo"))).all();
ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class); ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class);
verify(template).determineCollectionName(captor.capture()); verify(template).getCollectionName(captor.capture());
verify(template).aggregate(any(Aggregation.class), eq("person"), captor.capture()); verify(template).aggregate(any(Aggregation.class), eq("person"), captor.capture());
assertThat(captor.getAllValues()).containsExactly(Person.class, Person.class); assertThat(captor.getAllValues()).containsExactly(Person.class, Person.class);
@@ -93,13 +93,13 @@ public class ReactiveAggregationOperationSupportUnitTests {
@Test // DATAMONGO-1719 @Test // DATAMONGO-1719
public void aggregateWithTypeAggregation() { public void aggregateWithTypeAggregation() {
when(template.determineCollectionName(any(Class.class))).thenReturn("person"); when(template.getCollectionName(any(Class.class))).thenReturn("person");
opSupport.aggregateAndReturn(Jedi.class).by(newAggregation(Person.class, project("foo"))).all(); opSupport.aggregateAndReturn(Jedi.class).by(newAggregation(Person.class, project("foo"))).all();
ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class); ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class);
verify(template).determineCollectionName(captor.capture()); verify(template).getCollectionName(captor.capture());
verify(template).aggregate(any(Aggregation.class), eq("person"), captor.capture()); verify(template).aggregate(any(Aggregation.class), eq("person"), captor.capture());
assertThat(captor.getAllValues()).containsExactly(Person.class, Jedi.class); assertThat(captor.getAllValues()).containsExactly(Person.class, Jedi.class);

View File

@@ -52,7 +52,7 @@ public class ReactiveInsertOperationSupportUnitTests {
@Before @Before
public void setUp() { public void setUp() {
when(template.determineCollectionName(any(Class.class))).thenReturn(STAR_WARS); when(template.getCollectionName(any(Class.class))).thenReturn(STAR_WARS);
ops = new ReactiveInsertOperationSupport(template); ops = new ReactiveInsertOperationSupport(template);
@@ -77,7 +77,7 @@ public class ReactiveInsertOperationSupportUnitTests {
ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class); ArgumentCaptor<Class> captor = ArgumentCaptor.forClass(Class.class);
verify(template).determineCollectionName(captor.capture()); verify(template).getCollectionName(captor.capture());
verify(template).insert(eq(luke), eq(STAR_WARS)); verify(template).insert(eq(luke), eq(STAR_WARS));
assertThat(captor.getAllValues()).containsExactly(Person.class); assertThat(captor.getAllValues()).containsExactly(Person.class);
@@ -88,7 +88,7 @@ public class ReactiveInsertOperationSupportUnitTests {
ops.insert(Person.class).inCollection(STAR_WARS).one(luke); ops.insert(Person.class).inCollection(STAR_WARS).one(luke);
verify(template, never()).determineCollectionName(any(Class.class)); verify(template, never()).getCollectionName(any(Class.class));
verify(template).insert(eq(luke), eq(STAR_WARS)); verify(template).insert(eq(luke), eq(STAR_WARS));
} }
@@ -97,7 +97,7 @@ public class ReactiveInsertOperationSupportUnitTests {
ops.insert(Person.class).all(Arrays.asList(luke, han)); ops.insert(Person.class).all(Arrays.asList(luke, han));
verify(template).determineCollectionName(any(Class.class)); verify(template).getCollectionName(any(Class.class));
verify(template).insert(anyList(), eq(STAR_WARS)); verify(template).insert(anyList(), eq(STAR_WARS));
} }

View File

@@ -55,7 +55,7 @@ public class ReactiveMapReduceOperationSupportUnitTests {
@Before @Before
public void setUp() { public void setUp() {
when(template.determineCollectionName(eq(Person.class))).thenReturn(STAR_WARS); when(template.getCollectionName(eq(Person.class))).thenReturn(STAR_WARS);
mapReduceOpsSupport = new ReactiveMapReduceOperationSupport(template); mapReduceOpsSupport = new ReactiveMapReduceOperationSupport(template);
} }

View File

@@ -708,7 +708,7 @@ public class ReactiveMongoTemplateTests {
.expectNextCount(3) // .expectNextCount(3) //
.verifyComplete(); .verifyComplete();
StepVerifier.create(template.remove(Mono.just(spring), template.determineCollectionName(Sample.class))) StepVerifier.create(template.remove(Mono.just(spring), template.getCollectionName(Sample.class)))
.expectNextCount(1).verifyComplete(); .expectNextCount(1).verifyComplete();
StepVerifier.create(template.count(new Query(), Sample.class)).expectNext(2L).verifyComplete(); StepVerifier.create(template.count(new Query(), Sample.class)).expectNext(2L).verifyComplete();
} }
@@ -756,7 +756,7 @@ public class ReactiveMongoTemplateTests {
dbObject.put("firstName", "Oliver"); dbObject.put("firstName", "Oliver");
StepVerifier.create(template.insert(dbObject, // StepVerifier.create(template.insert(dbObject, //
template.determineCollectionName(PersonWithVersionPropertyOfTypeInteger.class))) // template.getCollectionName(PersonWithVersionPropertyOfTypeInteger.class))) //
.expectNextCount(1) // .expectNextCount(1) //
.verifyComplete(); .verifyComplete();
} }