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-09 00:09:38 +01:00
parent bf58e9536b
commit e4103eacda
2 changed files with 21 additions and 3 deletions

View File

@@ -103,7 +103,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements MongoR
}
if (allNew) {
mongoOperations.insertAll(result);
mongoOperations.insert(result, entityInformation.getCollectionName());
} else {
for (S entity : result) {

View File

@@ -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<String, Person> references, List<Person> saved) {
for (Person person : saved) {