Skip to content

Commit 19e08a5

Browse files
committed
DATAMONGO-970 - MongoTemplate.remove(…) now correctly builds query for DBObjects.
If a DBObject was handed into MongoTemplate.remove(…) we previously failed to look up the id value to create a by-id-query. This commit adds explicit handling of DBObjects by looking up their _id field to obtain the id value.
1 parent 6389b1b commit 19e08a5

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,17 +1070,22 @@ public WriteResult remove(Object object, String collection) {
10701070
}
10711071

10721072
/**
1073-
* Returns {@link Entry} containing the {@link MongoPersistentProperty} defining the {@literal id} as
1074-
* {@link Entry#getKey()} and the {@link Id}s property value as its {@link Entry#getValue()}.
1073+
* Returns {@link Entry} containing the field name of the id property as {@link Entry#getKey()} and the {@link Id}s
1074+
* property value as its {@link Entry#getValue()}.
10751075
*
10761076
* @param object
10771077
* @return
10781078
*/
1079-
private Map.Entry<MongoPersistentProperty, Object> extractIdPropertyAndValue(Object object) {
1079+
private Entry<String, Object> extractIdPropertyAndValue(Object object) {
10801080

10811081
Assert.notNull(object, "Id cannot be extracted from 'null'.");
10821082

10831083
Class<?> objectType = object.getClass();
1084+
1085+
if (object instanceof DBObject) {
1086+
return Collections.singletonMap(ID_FIELD, ((DBObject) object).get(ID_FIELD)).entrySet().iterator().next();
1087+
}
1088+
10841089
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(objectType);
10851090
MongoPersistentProperty idProp = entity == null ? null : entity.getIdProperty();
10861091

@@ -1090,7 +1095,7 @@ private Map.Entry<MongoPersistentProperty, Object> extractIdPropertyAndValue(Obj
10901095

10911096
Object idValue = BeanWrapper.create(object, mongoConverter.getConversionService())
10921097
.getProperty(idProp, Object.class);
1093-
return Collections.singletonMap(idProp, idValue).entrySet().iterator().next();
1098+
return Collections.singletonMap(idProp.getFieldName(), idValue).entrySet().iterator().next();
10941099
}
10951100

10961101
/**
@@ -1101,8 +1106,8 @@ private Map.Entry<MongoPersistentProperty, Object> extractIdPropertyAndValue(Obj
11011106
*/
11021107
private Query getIdQueryFor(Object object) {
11031108

1104-
Map.Entry<MongoPersistentProperty, Object> id = extractIdPropertyAndValue(object);
1105-
return new Query(where(id.getKey().getFieldName()).is(id.getValue()));
1109+
Entry<String, Object> id = extractIdPropertyAndValue(object);
1110+
return new Query(where(id.getKey()).is(id.getValue()));
11061111
}
11071112

11081113
/**
@@ -1116,7 +1121,7 @@ private Query getIdInQueryFor(Collection<?> objects) {
11161121
Assert.notEmpty(objects, "Cannot create Query for empty collection.");
11171122

11181123
Iterator<?> it = objects.iterator();
1119-
Map.Entry<MongoPersistentProperty, Object> firstEntry = extractIdPropertyAndValue(it.next());
1124+
Entry<String, Object> firstEntry = extractIdPropertyAndValue(it.next());
11201125

11211126
ArrayList<Object> ids = new ArrayList<Object>(objects.size());
11221127
ids.add(firstEntry.getValue());
@@ -1125,7 +1130,7 @@ private Query getIdInQueryFor(Collection<?> objects) {
11251130
ids.add(extractIdPropertyAndValue(it.next()).getValue());
11261131
}
11271132

1128-
return new Query(where(firstEntry.getKey().getFieldName()).in(ids));
1133+
return new Query(where(firstEntry.getKey()).in(ids));
11291134
}
11301135

11311136
private void assertUpdateableIdIfNotSet(Object entity) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,6 +2693,22 @@ public void shouldReuseExistingDBRefInQueryFromDbRefAssociationAfterLoad() {
26932693
assertThat(result.getContent().getName(), is(content.getName()));
26942694
}
26952695

2696+
/**
2697+
* @see DATAMONGO-970
2698+
*/
2699+
@Test
2700+
public void insertsAndRemovesBasicDbObjectCorrectly() {
2701+
2702+
BasicDBObject object = new BasicDBObject("key", "value");
2703+
template.insert(object, "collection");
2704+
2705+
assertThat(object.get("_id"), is(notNullValue()));
2706+
assertThat(template.findAll(DBObject.class, "collection"), hasSize(1));
2707+
2708+
template.remove(object, "collection");
2709+
assertThat(template.findAll(DBObject.class, "collection"), hasSize(0));
2710+
}
2711+
26962712
static class DoucmentWithNamedIdField {
26972713

26982714
@Id String someIdKey;

0 commit comments

Comments
 (0)