diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java index 210fffcbe..4b93f2a9d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java @@ -1754,11 +1754,10 @@ public interface MongoOperations extends FluentMongoOperations { List findAllAndRemove(Query query, Class entityClass, String collectionName); /** - * Triggers replaceOne to - * replace a single document matching {@link Criteria} of given {@link Query} with the {@code replacement} document. + * Replace a single document matching the {@link Criteria} of given {@link Query} with the {@code replacement} document. *
* The collection name is derived from the {@literal replacement} type.
- * Options are defaulted to {@link ReplaceOptions#empty()}.
+ * Options are defaulted to {@link ReplaceOptions#none()}.
* NOTE: The replacement entity must not hold an {@literal id}. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a record and also an optional @@ -1767,16 +1766,15 @@ public interface MongoOperations extends FluentMongoOperations { * @return the {@link UpdateResult} which lets you access the results of the previous replacement. * @throws org.springframework.data.mapping.MappingException if the collection name cannot be * {@link #getCollectionName(Class) derived} from the given replacement value. + * @since 4.2 */ default UpdateResult replace(Query query, T replacement) { - return replace(query, replacement, ReplaceOptions.empty()); + return replace(query, replacement, ReplaceOptions.none()); } /** - * Triggers replaceOne to - * replace a single document matching {@link Criteria} of given {@link Query} with the {@code replacement} - * document.
- * Options are defaulted to {@link ReplaceOptions#empty()}.
+ * Replace a single document matching the {@link Criteria} of given {@link Query} with the {@code replacement} document. + * Options are defaulted to {@link ReplaceOptions#none()}.
* NOTE: The replacement entity must not hold an {@literal id}. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a record and also an optional @@ -1786,42 +1784,43 @@ public interface MongoOperations extends FluentMongoOperations { * @return the {@link UpdateResult} which lets you access the results of the previous replacement. * @throws org.springframework.data.mapping.MappingException if the collection name cannot be * {@link #getCollectionName(Class) derived} from the given replacement value. + * @since 4.2 */ default UpdateResult replace(Query query, T replacement, String collectionName) { - return replace(query, replacement, ReplaceOptions.empty(), collectionName); + return replace(query, replacement, ReplaceOptions.none(), collectionName); } /** - * Triggers replaceOne to - * replace a single document matching {@link Criteria} of given {@link Query} with the {@code replacement} document + * Replace a single document matching the {@link Criteria} of given {@link Query} with the {@code replacement} document * taking {@link ReplaceOptions} into account.
* NOTE: The replacement entity must not hold an {@literal id}. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a record and also an optional * fields specification. Must not be {@literal null}. * @param replacement the replacement document. Must not be {@literal null}. - * @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}. + * @param options the {@link ReplaceOptions} holding additional information. Must not be {@literal null}. * @return the {@link UpdateResult} which lets you access the results of the previous replacement. * @throws org.springframework.data.mapping.MappingException if the collection name cannot be * {@link #getCollectionName(Class) derived} from the given replacement value. + * @since 4.2 */ default UpdateResult replace(Query query, T replacement, ReplaceOptions options) { return replace(query, replacement, options, getCollectionName(ClassUtils.getUserClass(replacement))); } /** - * Triggers replaceOne to - * replace a single document matching {@link Criteria} of given {@link Query} with the {@code replacement} document + * Replace a single document matching the {@link Criteria} of given {@link Query} with the {@code replacement} document * taking {@link ReplaceOptions} into account.
* NOTE: The replacement entity must not hold an {@literal id}. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a record and also an optional * fields specification. Must not be {@literal null}. * @param replacement the replacement document. Must not be {@literal null}. - * @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}. + * @param options the {@link ReplaceOptions} holding additional information. Must not be {@literal null}. * @return the {@link UpdateResult} which lets you access the results of the previous replacement. * @throws org.springframework.data.mapping.MappingException if the collection name cannot be * {@link #getCollectionName(Class) derived} from the given replacement value. + * @since 4.2 */ default UpdateResult replace(Query query, T replacement, ReplaceOptions options, String collectionName) { @@ -1830,41 +1829,38 @@ public interface MongoOperations extends FluentMongoOperations { } /** - * Triggers replaceOne to - * replace a single document matching {@link Criteria} of given {@link Query} with the {@code replacement} document + * Replace a single document matching the {@link Criteria} of given {@link Query} with the {@code replacement} document * taking {@link ReplaceOptions} into account.
* NOTE: The replacement entity must not hold an {@literal id}. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a record and also an optional * fields specification. Must not be {@literal null}. * @param replacement the replacement document. Must not be {@literal null}. - * @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}. + * @param options the {@link ReplaceOptions} holding additional information. Must not be {@literal null}. * @param entityType the type used for mapping the {@link Query} to domain type fields and deriving the collection * from. Must not be {@literal null}. * @return the {@link UpdateResult} which lets you access the results of the previous replacement. * @throws org.springframework.data.mapping.MappingException if the collection name cannot be * {@link #getCollectionName(Class) derived} from the given replacement value. + * @since 4.2 */ default UpdateResult replace(Query query, S replacement, ReplaceOptions options, Class entityType) { - return replace(query, replacement, options, entityType, getCollectionName(ClassUtils.getUserClass(entityType))); } /** - * Triggers replaceOne to - * replace a single document matching {@link Criteria} of given {@link Query} with the {@code replacement} document + * Replace a single document matching the {@link Criteria} of given {@link Query} with the {@code replacement} document * taking {@link ReplaceOptions} into account.
* NOTE: The replacement entity must not hold an {@literal id}. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a record and also an optional * fields specification. Must not be {@literal null}. * @param replacement the replacement document. Must not be {@literal null}. - * @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}. + * @param options the {@link ReplaceOptions} holding additional information. Must not be {@literal null}. * @param entityType the type used for mapping the {@link Query} to domain type fields. Must not be {@literal null}. * @param collectionName the collection to query. Must not be {@literal null}. * @return the {@link UpdateResult} which lets you access the results of the previous replacement. - * @throws org.springframework.data.mapping.MappingException if the collection name cannot be - * {@link #getCollectionName(Class) derived} from the given replacement value. + * @since 4.2 */ UpdateResult replace(Query query, S replacement, ReplaceOptions options, Class entityType, String collectionName); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index b2d0d0461..b8286c04e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -3425,9 +3425,10 @@ public class MongoTemplate @Override public UpdateResult replace(Query query, S replacement, ReplaceOptions options, Class entityType, String collectionName) { + Assert.notNull(query, "Query must not be null"); Assert.notNull(replacement, "Replacement must not be null"); - Assert.notNull(options, "Options must not be null Use ReplaceOptions#empty() instead"); + Assert.notNull(options, "Options must not be null Use ReplaceOptions#none() instead"); Assert.notNull(entityType, "EntityType must not be null"); Assert.notNull(collectionName, "CollectionName must not be null"); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReplaceOptions.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReplaceOptions.java index f5a0d0eab..e1fe9017b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReplaceOptions.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReplaceOptions.java @@ -1,18 +1,35 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import org.springframework.data.mongodb.core.query.Query; + /** - * Options for - * replaceOne. - *
- * Defaults to + * Options for {@link org.springframework.data.mongodb.core.MongoOperations#replace(Query, Object) replace operations}. Defaults to *
*
upsert
*
false
*
* * @author Jakub Zurawa + * @author Christoph Strob + * @since 4.2 */ -package org.springframework.data.mongodb.core; - public class ReplaceOptions { + private boolean upsert; private static final ReplaceOptions NONE = new ReplaceOptions() { @@ -42,25 +59,11 @@ public class ReplaceOptions { * Static factory method returning an unmodifiable {@link ReplaceOptions} instance. * * @return unmodifiable {@link ReplaceOptions} instance. - * @since 2.2 */ public static ReplaceOptions none() { return NONE; } - /** - * Static factory method to create a {@link ReplaceOptions} instance with - *
- *
upsert
- *
false
- *
- * - * @return new instance of {@link ReplaceOptions}. - */ - public static ReplaceOptions empty() { - return new ReplaceOptions(); - } - /** * Insert a new document if not exists. *