DATAMONGO-629 - Fixed QueryMapper not to massage queries without type information.
So far the QueryMapper applied the id massaging (especially interpreting the default id keys) even if there was no persistence metadata available to do so. This caused e.g. queries handed into MongoTemplate.count(Query, String) to get keys of "id" massaged into "_id" which shouldn't be the case as we cannot assume anything about the documents and the keys contained in them. So we now only apply the defaults if there is at least persistence metadata present. This means that for methods on MongoOperations that don't take type information of any kind the queries have to be defined in terms of the document, not the object model as we cannot refer to it.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2012 the original author or authors.
|
||||
* Copyright 2011-2013 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.
|
||||
@@ -214,7 +214,11 @@ public class QueryMapper {
|
||||
*/
|
||||
private String determineKey(String key, MongoPersistentEntity<?> entity) {
|
||||
|
||||
if (entity == null && DEFAULT_ID_NAMES.contains(key)) {
|
||||
if (entity == null) {
|
||||
return key;
|
||||
}
|
||||
|
||||
if (!entity.hasIdProperty() && DEFAULT_ID_NAMES.contains(key)) {
|
||||
return "_id";
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core.mapping;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -51,7 +52,7 @@ public class MongoMappingContext extends AbstractMappingContext<BasicMongoPersis
|
||||
*/
|
||||
@Override
|
||||
protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> type) {
|
||||
return !MongoSimpleTypes.HOLDER.isSimpleType(type.getType());
|
||||
return !MongoSimpleTypes.HOLDER.isSimpleType(type.getType()) && !type.getType().equals(HashMap.class);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1517,6 +1517,22 @@ public class MongoTemplateTests {
|
||||
template.save(person);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-629
|
||||
*/
|
||||
@Test
|
||||
public void countAndFindWithoutTypeInformation() {
|
||||
|
||||
Person person = new Person();
|
||||
template.save(person);
|
||||
|
||||
Query query = query(where("_id").is(person.getId()));
|
||||
String collectionName = template.getCollectionName(Person.class);
|
||||
|
||||
assertThat(template.find(query, HashMap.class, collectionName), hasSize(1));
|
||||
assertThat(template.count(query, collectionName), is(1L));
|
||||
}
|
||||
|
||||
static class MyId {
|
||||
|
||||
String first;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2012 the original author or authors.
|
||||
* Copyright 2011-2013 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.
|
||||
@@ -347,6 +347,22 @@ public class QueryMapperUnitTests {
|
||||
assertThat(object.get("reference"), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-629
|
||||
*/
|
||||
@Test
|
||||
public void doesNotMapIdIfNoEntityMetadataAvailable() {
|
||||
|
||||
String id = new ObjectId().toString();
|
||||
Query query = query(where("id").is(id));
|
||||
|
||||
DBObject object = mapper.getMappedObject(query.getQueryObject(), null);
|
||||
|
||||
assertThat(object.containsField("id"), is(true));
|
||||
assertThat(object.get("id"), is((Object) id));
|
||||
assertThat(object.containsField("_id"), is(false));
|
||||
}
|
||||
|
||||
class IdWrapper {
|
||||
Object id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user