DATAMONGO-1896 - SimpleMongoRepository.saveAll(…) now consistently uses aggregate collection for inserts.

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.
This commit is contained in:
Oliver Gierke
2018-03-08 23:51:17 +01:00
parent dc31f4f32f
commit 14b49638a0
2 changed files with 20 additions and 2 deletions

View File

@@ -100,7 +100,7 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
if (allNew) { if (allNew) {
List<S> result = source.stream().collect(Collectors.toList()); List<S> result = source.stream().collect(Collectors.toList());
mongoOperations.insertAll(result); mongoOperations.insert(result, entityInformation.getCollectionName());
return result; return result;
} else { } else {

View File

@@ -32,9 +32,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example; import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.ExampleMatcher.*;
import org.springframework.data.geo.Point; import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint; import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
@@ -365,6 +365,24 @@ public class SimpleMongoRepositoryTests {
assertThat(repository.count(Example.of(sample))).isEqualTo(2L); 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<String, Person> references, List<Person> saved) { private void assertThatAllReferencePersonsWereStoredCorrectly(Map<String, Person> references, List<Person> saved) {
for (Person person : saved) { for (Person person : saved) {