diff --git a/pom.xml b/pom.xml index 75745189ed..02990bc5ab 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.7.0.BUILD-SNAPSHOT + 1.7.0.DATAMONGO-1054-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-cross-store/pom.xml b/spring-data-mongodb-cross-store/pom.xml index 40e8a0f253..8ba49dbd9b 100644 --- a/spring-data-mongodb-cross-store/pom.xml +++ b/spring-data-mongodb-cross-store/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-mongodb-parent - 1.7.0.BUILD-SNAPSHOT + 1.7.0.DATAMONGO-1054-SNAPSHOT ../pom.xml @@ -48,7 +48,7 @@ org.springframework.data spring-data-mongodb - 1.7.0.BUILD-SNAPSHOT + 1.7.0.DATAMONGO-1054-SNAPSHOT diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 13110137b6..f5fb8e2623 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 1.7.0.BUILD-SNAPSHOT + 1.7.0.DATAMONGO-1054-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-log4j/pom.xml b/spring-data-mongodb-log4j/pom.xml index 6ff09e4577..2cb21ec33c 100644 --- a/spring-data-mongodb-log4j/pom.xml +++ b/spring-data-mongodb-log4j/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.7.0.BUILD-SNAPSHOT + 1.7.0.DATAMONGO-1054-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 568d347c4d..2842763951 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ org.springframework.data spring-data-mongodb-parent - 1.7.0.BUILD-SNAPSHOT + 1.7.0.DATAMONGO-1054-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepository.java index 8b32178ca6..5560f3b28c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepository.java @@ -27,6 +27,7 @@ * * @author Oliver Gierke * @author Christoph Strobl + * @author Thomas Darimont */ @NoRepositoryBean public interface MongoRepository extends PagingAndSortingRepository { @@ -48,4 +49,30 @@ public interface MongoRepository extends PagingAndSo * @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort) */ List findAll(Sort sort); + + /** + * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the + * entity instance completely. + *

+ * This uses {@link org.springframework.data.mongodb.core.MongoTemplate#insert(Object)} for storing the given entity. + *

+ * Note that this method does neither fire any save events nor performs any id population or version checking. + * + * @param entity + * @return the saved entity + */ + S insert(S entity); + + /** + * Saves all given entities. + *

+ * This uses {@link org.springframework.data.mongodb.core.MongoTemplate#insert(Object)} for storing the given entity. + *

+ * Note that this method does neither fire any save events nor nor performs any id population or version checking. + * + * @param entities + * @return the saved entities + * @throws IllegalArgumentException in case the given entity is (@literal null}. + */ + List insert(Iterable entities); } 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 42ee34565e..0377893ed2 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 @@ -19,6 +19,7 @@ import java.io.Serializable; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -41,6 +42,7 @@ * * @author Oliver Gierke * @author Christoph Strobl + * @author Thomas Darimont */ public class SimpleMongoRepository implements MongoRepository { @@ -48,7 +50,7 @@ public class SimpleMongoRepository implements MongoR private final MongoEntityInformation entityInformation; /** - * Creates a ew {@link SimpleMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}. + * Creates a new {@link SimpleMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}. * * @param metadata must not be {@literal null}. * @param template must not be {@literal null}. @@ -82,7 +84,7 @@ public List save(Iterable entities) { Assert.notNull(entities, "The given Iterable of entities not be null!"); - List result = new ArrayList(); + List result = new ArrayList(tryDetermineRealSizeOrReturn(entities, 10)); for (S entity : entities) { save(entity); @@ -181,7 +183,7 @@ public List findAll() { */ public Iterable findAll(Iterable ids) { - Set parameters = new HashSet(); + Set parameters = new HashSet(tryDetermineRealSizeOrReturn(ids, 10)); for (ID id : ids) { parameters.add(id); } @@ -234,4 +236,57 @@ protected MongoEntityInformation getEntityInformation() { return entityInformation; } + /* (non-Javadoc) + * @see org.springframework.data.mongodb.repository.MongoRepository#insert(java.lang.Object) + */ + @Override + public S insert(S entity) { + + Assert.notNull(entity, "Entity must not be null!"); + + mongoOperations.insert(entity, entityInformation.getCollectionName()); + return entity; + } + + /* (non-Javadoc) + * @see org.springframework.data.mongodb.repository.MongoRepository#insert(java.lang.Iterable) + */ + @Override + public List insert(Iterable entities) { + + Assert.notNull(entities, "The given Iterable of entities not be null!"); + + List list = convertIterableToList(entities); + + if (list.isEmpty()) { + return list; + } + + mongoOperations.insertAll(list); + return list; + } + + private List convertIterableToList(Iterable entities) { + + if (entities instanceof List) { + return (List) entities; + } + + int capacity = tryDetermineRealSizeOrReturn(entities, 10); + + if (capacity == 0 || entities == null) { + return Collections. emptyList(); + } + + List list = new ArrayList(capacity); + for (S entity : entities) { + list.add(entity); + } + + return list; + } + + private int tryDetermineRealSizeOrReturn(Iterable iterable, int defaultSize) { + return iterable == null ? 0 : (iterable instanceof Collection) ? ((Collection) iterable).size() : defaultSize; + } } 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 4c3600d671..2f8f6e75f6 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 @@ -1,5 +1,5 @@ /* - * Copyright 2010-2012 the original author or authors. + * Copyright 2010-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,16 @@ package org.springframework.data.mongodb.repository.support; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.*; +import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; import org.junit.Before; import org.junit.Test; @@ -34,13 +40,13 @@ /** * @author A. B. M. Kowser + * @author Thomas Darimont */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:infrastructure.xml") public class SimpleMongoRepositoryTests { - @Autowired - private MongoTemplate template; + @Autowired private MongoTemplate template; private Person oliver, dave, carter, boyd, stefan, leroi, alicia; private List all; @@ -69,7 +75,7 @@ public void findALlFromCustomCollectionName() { List result = repository.findAll(); assertThat(result, hasSize(all.size())); } - + @Test public void findOneFromCustomCollectionName() { Person result = repository.findOne(dave.getId()); @@ -94,6 +100,76 @@ public void deleteByIdFromCustomCollectionName() { assertThat(result, not(hasItem(dave))); } + /** + * @see DATAMONGO-1054 + */ + @Test + public void shouldInsertSingle() { + + String randomId = UUID.randomUUID().toString(); + + Person person1 = new Person("First1" + randomId, "Last2" + randomId, 42); + person1 = repository.insert(person1); + + Person saved = repository.findOne(person1.getId()); + + assertThat(saved, is(equalTo(person1))); + } + + /** + * @see DATAMONGO-1054 + */ + @Test + public void shouldInsertMutlipleFromList() { + + String randomId = UUID.randomUUID().toString(); + + Map idToPerson = new HashMap(); + List persons = new ArrayList(); + for (int i = 0; i < 10; i++) { + Person person = new Person("First" + i + randomId, "Last" + randomId + i, 42 + i); + idToPerson.put(person.getId(), person); + persons.add(person); + } + + List saved = repository.insert(persons); + + assertThat(saved, hasSize(persons.size())); + + assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved); + } + + /** + * @see DATAMONGO-1054 + */ + @Test + public void shouldInsertMutlipleFromSet() { + + String randomId = UUID.randomUUID().toString(); + + Map idToPerson = new HashMap(); + Set persons = new HashSet(); + for (int i = 0; i < 10; i++) { + Person person = new Person("First" + i + randomId, "Last" + i + randomId, 42 + i); + idToPerson.put(person.getId(), person); + persons.add(person); + } + + List saved = repository.insert(persons); + + assertThat(saved, hasSize(persons.size())); + + assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved); + } + + private void assertThatAllReferencePersonsWereStoredCorrectly(Map references, List saved) { + + for (Person person : saved) { + Person reference = references.get(person.getId()); + assertThat(person, is(equalTo(reference))); + } + } + private static class CustomizedPersonInformation implements MongoEntityInformation { @Override