DATAMONGO-2652 - Implements CrudRepository and ReactiveCrudRepository.delete(Iterable<ID> ids).
See also: DATACMNS-800. Original pull request: #892.
This commit is contained in:
committed by
Mark Paluch
parent
5c3bb00b24
commit
c1a8ffec96
@@ -51,6 +51,7 @@ import com.mongodb.client.result.DeleteResult;
|
||||
* @author Thomas Darimont
|
||||
* @author Mark Paluch
|
||||
* @author Mehran Behnam
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
|
||||
|
||||
@@ -182,11 +183,19 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
|
||||
@Override
|
||||
public void deleteAll(Iterable<? extends T> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities not be null!");
|
||||
Assert.notNull(entities, "The given Iterable of entities must not be null!");
|
||||
|
||||
entities.forEach(this::delete);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAllById(Iterable<? extends ID> ids) {
|
||||
|
||||
Assert.notNull(ids, "The given Iterable of ids must not be null!");
|
||||
|
||||
ids.forEach(this::deleteById);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#deleteAll()
|
||||
|
||||
@@ -17,10 +17,8 @@ package org.springframework.data.mongodb.repository.support;
|
||||
|
||||
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -40,6 +38,9 @@ import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.client.result.DeleteResult;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* Reactive repository base implementation for Mongo.
|
||||
*
|
||||
@@ -47,6 +48,7 @@ import com.mongodb.client.result.DeleteResult;
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
* @author Ruben J Garcia
|
||||
* @author Jens Schauder
|
||||
* @since 2.0
|
||||
*/
|
||||
public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> {
|
||||
@@ -401,7 +403,28 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities must not be null!");
|
||||
|
||||
return Flux.fromIterable(entities).flatMap(this::delete).then();
|
||||
Collection<?> idCollection = StreamUtils.createStreamFromIterator(entities.iterator())
|
||||
.map(entityInformation::getId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Criteria idsInCriteria = where(entityInformation.getIdAttribute()).in(idCollection);
|
||||
|
||||
return mongoOperations
|
||||
.remove(new Query(idsInCriteria), entityInformation.getJavaType(), entityInformation.getCollectionName())
|
||||
.then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> deleteAllById(Iterable<? extends ID> ids) {
|
||||
|
||||
Assert.notNull(ids, "The given Iterable of ids must not be null!");
|
||||
|
||||
Collection<?> idCollection = StreamUtils.createStreamFromIterator(ids.iterator()).collect(Collectors.toList());
|
||||
Criteria idsInCriteria = where(entityInformation.getIdAttribute()).in(idCollection);
|
||||
|
||||
return mongoOperations
|
||||
.remove(new Query(idsInCriteria), entityInformation.getJavaType(), entityInformation.getCollectionName())
|
||||
.then();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.repository;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.domain.Sort.Direction.*;
|
||||
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
||||
@@ -66,7 +67,6 @@ import org.springframework.data.mongodb.test.util.MongoClientExtension;
|
||||
import org.springframework.data.mongodb.test.util.MongoTestUtils;
|
||||
import org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
|
||||
import org.springframework.data.repository.query.ReactiveQueryMethodEvaluationContextProvider;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@@ -77,10 +77,12 @@ import com.mongodb.reactivestreams.client.MongoClient;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@ExtendWith({ MongoClientExtension.class, SpringExtension.class })
|
||||
public class ReactiveMongoRepositoryTests {
|
||||
|
||||
public static final int PERSON_COUNT = 7;
|
||||
static @Client MongoClient mongoClient;
|
||||
|
||||
@Autowired ReactiveMongoTemplate template;
|
||||
@@ -154,17 +156,17 @@ public class ReactiveMongoRepositoryTests {
|
||||
dave = new Person("Dave", "Matthews", 42);
|
||||
oliver = new Person("Oliver August", "Matthews", 4);
|
||||
carter = new Person("Carter", "Beauford", 49);
|
||||
carter.setSkills(Arrays.asList("Drums", "percussion", "vocals"));
|
||||
carter.setSkills(asList("Drums", "percussion", "vocals"));
|
||||
Thread.sleep(10);
|
||||
boyd = new Person("Boyd", "Tinsley", 45);
|
||||
boyd.setSkills(Arrays.asList("Violin", "Electric Violin", "Viola", "Mandolin", "Vocals", "Guitar"));
|
||||
boyd.setSkills(asList("Violin", "Electric Violin", "Viola", "Mandolin", "Vocals", "Guitar"));
|
||||
stefan = new Person("Stefan", "Lessard", 34);
|
||||
leroi = new Person("Leroi", "Moore", 41);
|
||||
|
||||
alicia = new Person("Alicia", "Keys", 30, Sex.FEMALE);
|
||||
|
||||
repository.saveAll(Arrays.asList(oliver, carter, boyd, stefan, leroi, alicia, dave)).as(StepVerifier::create) //
|
||||
.expectNextCount(7) //
|
||||
repository.saveAll(asList(oliver, carter, boyd, stefan, leroi, alicia, dave)).as(StepVerifier::create) //
|
||||
.expectNextCount(PERSON_COUNT) //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@@ -411,7 +413,7 @@ public class ReactiveMongoRepositoryTests {
|
||||
|
||||
leroi.id = null;
|
||||
boyd.id = null;
|
||||
contactRepository.saveAll(Arrays.asList(leroi, boyd)) //
|
||||
contactRepository.saveAll(asList(leroi, boyd)) //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNextCount(2) //
|
||||
.verifyComplete();
|
||||
@@ -430,7 +432,7 @@ public class ReactiveMongoRepositoryTests {
|
||||
@Test // DATAMONGO-2182
|
||||
public void shouldFindPersonsWhenUsingQueryDslPerdicatedOnIdProperty() {
|
||||
|
||||
repository.findAll(person.id.in(Arrays.asList(dave.id, carter.id))) //
|
||||
repository.findAll(person.id.in(asList(dave.id, carter.id))) //
|
||||
.collectList() //
|
||||
.as(StepVerifier::create) //
|
||||
.assertNext(actual -> {
|
||||
@@ -468,7 +470,7 @@ public class ReactiveMongoRepositoryTests {
|
||||
.contains(new PersonAggregate("Tinsley", "Boyd")) //
|
||||
.contains(new PersonAggregate("Beauford", "Carter")) //
|
||||
.contains(new PersonAggregate("Moore", "Leroi")) //
|
||||
.contains(new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")));
|
||||
.contains(new PersonAggregate("Matthews", asList("Dave", "Oliver August")));
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@@ -484,7 +486,7 @@ public class ReactiveMongoRepositoryTests {
|
||||
new PersonAggregate("Beauford", "Carter"), //
|
||||
new PersonAggregate("Keys", "Alicia"), //
|
||||
new PersonAggregate("Lessard", "Stefan"), //
|
||||
new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")), //
|
||||
new PersonAggregate("Matthews", asList("Dave", "Oliver August")), //
|
||||
new PersonAggregate("Moore", "Leroi"), //
|
||||
new PersonAggregate("Tinsley", "Boyd"));
|
||||
}) //
|
||||
@@ -501,7 +503,7 @@ public class ReactiveMongoRepositoryTests {
|
||||
assertThat(actual) //
|
||||
.containsExactly( //
|
||||
new PersonAggregate("Lessard", "Stefan"), //
|
||||
new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")));
|
||||
new PersonAggregate("Matthews", asList("Dave", "Oliver August")));
|
||||
}) //
|
||||
.verifyComplete();
|
||||
}
|
||||
@@ -576,7 +578,7 @@ public class ReactiveMongoRepositoryTests {
|
||||
Person p3 = new Person(firstname, null);
|
||||
p3.setEmail("p3@example.com");
|
||||
|
||||
repository.saveAll(Arrays.asList(p1, p2, p3)).then().as(StepVerifier::create).verifyComplete();
|
||||
repository.saveAll(asList(p1, p2, p3)).then().as(StepVerifier::create).verifyComplete();
|
||||
|
||||
repository.projectToLastnameAndRemoveId(firstname) //
|
||||
.as(StepVerifier::create) //
|
||||
@@ -617,6 +619,18 @@ public class ReactiveMongoRepositoryTests {
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2652
|
||||
public void deleteAllById() {
|
||||
|
||||
repository.deleteAllById(asList(carter.id, dave.id)) //
|
||||
.as(StepVerifier::create) //
|
||||
.verifyComplete();
|
||||
|
||||
repository.count().as(StepVerifier::create) //
|
||||
.expectNext(PERSON_COUNT - 2L) //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
interface ReactivePersonRepository
|
||||
extends ReactiveMongoRepository<Person, String>, ReactiveQuerydslPredicateExecutor<Person> {
|
||||
|
||||
|
||||
@@ -15,11 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.repository.support;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.domain.ExampleMatcher.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -58,6 +58,7 @@ import org.springframework.transaction.support.TransactionTemplate;
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@ExtendWith({ MongoTemplateExtension.class, MongoServerCondition.class })
|
||||
public class SimpleMongoRepositoryTests {
|
||||
@@ -85,11 +86,11 @@ public class SimpleMongoRepositoryTests {
|
||||
leroi = new Person("Leroi", "Moore", 41);
|
||||
alicia = new Person("Alicia", "Keys", 30, Sex.FEMALE);
|
||||
|
||||
all = repository.saveAll(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia));
|
||||
all = repository.saveAll(asList(oliver, dave, carter, boyd, stefan, leroi, alicia));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findALlFromCustomCollectionName() {
|
||||
public void findAllFromCustomCollectionName() {
|
||||
assertThat(repository.findAll()).hasSize(all.size());
|
||||
}
|
||||
|
||||
@@ -384,7 +385,7 @@ public class SimpleMongoRepositoryTests {
|
||||
|
||||
repository.deleteAll();
|
||||
|
||||
repository.saveAll(Arrays.asList(first, second));
|
||||
repository.saveAll(asList(first, second));
|
||||
|
||||
assertThat(repository.findAll()).containsExactlyInAnyOrder(first, second);
|
||||
}
|
||||
@@ -435,6 +436,17 @@ public class SimpleMongoRepositoryTests {
|
||||
assertThat(exists).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2652
|
||||
public void deleteAllByIds() {
|
||||
|
||||
repository.deleteAllById(asList(dave.getId(), carter.getId()));
|
||||
|
||||
assertThat(repository.findAll()) //
|
||||
.hasSize(all.size() - 2) //
|
||||
.doesNotContain(dave) //
|
||||
.doesNotContain(carter);
|
||||
}
|
||||
|
||||
private void assertThatAllReferencePersonsWereStoredCorrectly(Map<String, Person> references, List<Person> saved) {
|
||||
|
||||
for (Person person : saved) {
|
||||
|
||||
Reference in New Issue
Block a user