Browse Source

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.
pull/553/head
Oliver Gierke 8 years ago
parent
commit
e4103eacda
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java
  2. 22
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java

2
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

@ -103,7 +103,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements MongoR @@ -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) {

22
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java

@ -33,17 +33,17 @@ import org.junit.Test; @@ -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 { @@ -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) {

Loading…
Cancel
Save