+ * 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 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 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