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) {