Skip to content

Commit 9059a77

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1534 - Fix bulk operations missing to write type info.
We now correctly convert entities into their MongoDB representation including type information via _class property. Original pull request: #415.
1 parent a741400 commit 9059a77

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultBulkOperations.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.data.util.Pair;
2626
import org.springframework.util.Assert;
2727

28+
import com.mongodb.BasicDBObject;
2829
import com.mongodb.BulkWriteException;
2930
import com.mongodb.BulkWriteOperation;
3031
import com.mongodb.BulkWriteRequestBuilder;
@@ -38,6 +39,7 @@
3839
*
3940
* @author Tobias Trelle
4041
* @author Oliver Gierke
42+
* @author Christoph Strobl
4143
* @since 1.9
4244
*/
4345
class DefaultBulkOperations implements BulkOperations {
@@ -117,7 +119,15 @@ public BulkOperations insert(Object document) {
117119

118120
Assert.notNull(document, "Document must not be null!");
119121

120-
bulk.insert((DBObject) mongoOperations.getConverter().convertToMongoType(document));
122+
if (document instanceof DBObject) {
123+
124+
bulk.insert((DBObject) document);
125+
return this;
126+
}
127+
128+
DBObject sink = new BasicDBObject();
129+
mongoOperations.getConverter().write(document, sink);
130+
bulk.insert(sink);
121131
return this;
122132
}
123133

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultBulkOperationsIntegrationTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
*
4747
* @author Tobias Trelle
4848
* @author Oliver Gierke
49+
* @author Christoph Strobl
4950
*/
5051
@RunWith(SpringJUnit4ClassRunner.class)
5152
@ContextConfiguration("classpath:infrastructure.xml")
@@ -270,6 +271,25 @@ public void mixedBulkOrderedWithList() {
270271
assertThat(result.getRemovedCount(), is(1));
271272
}
272273

274+
/**
275+
* @see DATAMONGO-1534
276+
*/
277+
@Test
278+
public void insertShouldConsiderInheritance() {
279+
280+
SpecialDoc specialDoc = new SpecialDoc();
281+
specialDoc.id = "id-special";
282+
specialDoc.value = "normal-value";
283+
specialDoc.specialValue = "special-value";
284+
285+
createBulkOps(BulkMode.ORDERED).insert(Arrays.asList(specialDoc)).execute();
286+
287+
BaseDoc doc = operations.findOne(where("_id", specialDoc.id), BaseDoc.class, COLLECTION_NAME);
288+
289+
assertThat(doc, notNullValue());
290+
assertThat(doc, instanceOf(SpecialDoc.class));
291+
}
292+
273293
private void testUpdate(BulkMode mode, boolean multi, int expectedUpdates) {
274294

275295
BulkOperations bulkOps = createBulkOps(mode);

0 commit comments

Comments
 (0)