From 7d5d0bbdfcd717bab7d73ee2243fb4d68ee20831 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 7 Jan 2019 08:40:35 +0100 Subject: [PATCH] =?UTF-8?q?DATAMONGO-2181=20-=20Consider=20repository=20co?= =?UTF-8?q?llection=20name=20in=20ReactiveMongoRepository.saveAll(?= =?UTF-8?q?=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now consider the collection name that is bound to the repository when inserting entities of which all are new. Previously, the collection name was derived from the entity. Original Pull Request: #632 --- .../SimpleReactiveMongoRepository.java | 4 +-- .../ReactiveMongoRepositoryTests.java | 35 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java index ca6a52264..6802a20c9 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java @@ -301,8 +301,8 @@ public class SimpleReactiveMongoRepository implement Streamable source = Streamable.of(entities); - return source.stream().allMatch(it -> entityInformation.isNew(it)) ? // - mongoOperations.insertAll(source.stream().collect(Collectors.toList())) : // + return source.stream().allMatch(entityInformation::isNew) ? // + mongoOperations.insert(source.stream().collect(Collectors.toList()), entityInformation.getCollectionName()) : // Flux.fromIterable(entities).flatMap(this::save); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ReactiveMongoRepositoryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ReactiveMongoRepositoryTests.java index 165321318..d1b711a90 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ReactiveMongoRepositoryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ReactiveMongoRepositoryTests.java @@ -15,7 +15,7 @@ */ package org.springframework.data.mongodb.repository; -import static org.assertj.core.api.Assertions.offset; +import static org.assertj.core.api.Assertions.*; import static org.springframework.data.domain.Sort.Direction.*; import static org.springframework.data.mongodb.test.util.Assertions.assertThat; @@ -78,6 +78,7 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF ClassLoader classLoader; BeanFactory beanFactory; ReactivePersonRepository repository; + ReactiveContactRepository contactRepository; ReactiveCappedCollectionRepository cappedRepository; Person dave, oliver, carter, boyd, stefan, leroi, alicia; @@ -102,6 +103,7 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF factory.setEvaluationContextProvider(QueryMethodEvaluationContextProvider.DEFAULT); repository = factory.getRepository(ReactivePersonRepository.class); + contactRepository = factory.getRepository(ReactiveContactRepository.class); cappedRepository = factory.getRepository(ReactiveCappedCollectionRepository.class); StepVerifier.create(repository.deleteAll()).verifyComplete(); @@ -345,6 +347,35 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF }).verifyComplete(); } + @Test // DATAMONGO-2181 + public void considersRepositoryCollectionName() { + + repository.deleteAll() // + .as(StepVerifier::create) // + .verifyComplete(); + + contactRepository.deleteAll() // + .as(StepVerifier::create) // + .verifyComplete(); + + leroi.id = null; + boyd.id = null; + contactRepository.saveAll(Arrays.asList(leroi, boyd)) // + .as(StepVerifier::create) // + .expectNextCount(2) // + .verifyComplete(); + + repository.count() // + .as(StepVerifier::create) // + .expectNext(0L) // + .verifyComplete(); + + contactRepository.count() // + .as(StepVerifier::create) // + .expectNext(2L) // + .verifyComplete(); + } + interface ReactivePersonRepository extends ReactiveMongoRepository { Flux findByLastname(String lastname); @@ -385,6 +416,8 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF Flux findByAgeGreaterThan(int age, Sort sort); } + interface ReactiveContactRepository extends ReactiveMongoRepository {} + interface ReactiveCappedCollectionRepository extends Repository { @Tailable