From 5b7b69026b15d27778a35130ddef1127b624a618 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 8 Mar 2018 23:51:17 +0100 Subject: [PATCH] =?UTF-8?q?DATAMONGO-1896=20-=20SimpleMongoRepository.save?= =?UTF-8?q?All(=E2=80=A6)=20now=20consistently=20uses=20aggregate=20collec?= =?UTF-8?q?tion=20for=20inserts.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We previously used MongoTemplate.insertAll(…) which determines the collection to insert the individual elements based on the type, which - in cases of entity inheritance - will use dedicated collections for sub-types of the aggregate root. Subsequent lookups of the entities will then fail, as those are executed against the collection the aggregate root is mapped to. We now rather use ….insert(Collection, String) handing the collection of the aggregate root explicitly. --- .../support/SimpleMongoRepository.java | 2 +- .../support/SimpleMongoRepositoryTests.java | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java index 1bdf87d6c..e8c2a400f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java @@ -100,7 +100,7 @@ public class SimpleMongoRepository implements MongoRepository { if (allNew) { List result = source.stream().collect(Collectors.toList()); - mongoOperations.insertAll(result); + mongoOperations.insert(result, entityInformation.getCollectionName()); return result; } else { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java index 0782a4356..8f2b94c3d 100755 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java @@ -32,9 +32,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; +import org.springframework.data.domain.ExampleMatcher.StringMatcher; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.ExampleMatcher.*; import org.springframework.data.geo.Point; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.geo.GeoJsonPoint; @@ -365,6 +365,24 @@ public class SimpleMongoRepositoryTests { assertThat(repository.count(Example.of(sample))).isEqualTo(2L); } + @Test // DATAMONGO-1896 + public void saveAllUsesEntityCollection() { + + Person first = new PersonExtended(); + first.setEmail("foo@bar.com"); + ReflectionTestUtils.setField(first, "id", null); + + Person second = new PersonExtended(); + second.setEmail("bar@foo.com"); + ReflectionTestUtils.setField(second, "id", null); + + repository.deleteAll(); + + repository.saveAll(Arrays.asList(first, second)); + + assertThat(repository.findAll()).containsExactlyInAnyOrder(first, second); + } + private void assertThatAllReferencePersonsWereStoredCorrectly(Map references, List saved) { for (Person person : saved) {