From e4103eacda812b8b9d1cd61cb772e83088e92ba3 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 9 Mar 2018 00:09:38 +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 | 22 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 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 c6549dff6..32e6f5ed1 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 @@ -103,7 +103,7 @@ public class SimpleMongoRepository implements MongoR } if (allNew) { - mongoOperations.insertAll(result); + mongoOperations.insert(result, entityInformation.getCollectionName()); } else { for (S entity : result) { 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 10aaa8564..9ca02afba 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 @@ -33,17 +33,17 @@ 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; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.repository.Address; import org.springframework.data.mongodb.repository.Person; -import org.springframework.data.mongodb.repository.User; import org.springframework.data.mongodb.repository.Person.Sex; +import org.springframework.data.mongodb.repository.User; import org.springframework.data.mongodb.repository.query.MongoEntityInformation; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -405,6 +405,24 @@ public class SimpleMongoRepositoryTests { assertThat(result, is(equalTo(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.save(Arrays.asList(first, second)); + + assertThat(repository.findAll(), containsInAnyOrder(first, second)); + } + private void assertThatAllReferencePersonsWereStoredCorrectly(Map references, List saved) { for (Person person : saved) {