Browse Source

DATAMONGO-1406 - Propagate PersistentEntity when mapping query criteria for nested keywords.

We now propagate the PersistentEntity when mapping nested keywords so that the criteria mapping chain for nested keywords and properties has now access to the PersistentEntity and can use configured field names.

Previously the plain property names have been used as field names and potential customizations via @Field have been ignored.

Original Pull Request: #384
pull/410/head
Mark Paluch 9 years ago committed by Christoph Strobl
parent
commit
a8751249fd
  1. 5
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  2. 35
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

5
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

@ -58,6 +58,7 @@ import com.mongodb.DBRef;
* @author Patryk Wasik * @author Patryk Wasik
* @author Thomas Darimont * @author Thomas Darimont
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch
*/ */
public class QueryMapper { public class QueryMapper {
@ -66,7 +67,7 @@ public class QueryMapper {
static final ClassTypeInformation<?> NESTED_DOCUMENT = ClassTypeInformation.from(NestedDocument.class); static final ClassTypeInformation<?> NESTED_DOCUMENT = ClassTypeInformation.from(NestedDocument.class);
private enum MetaMapping { private enum MetaMapping {
FORCE, WHEN_PRESENT, IGNORE; FORCE, WHEN_PRESENT, IGNORE
} }
private final ConversionService conversionService; private final ConversionService conversionService;
@ -316,7 +317,7 @@ public class QueryMapper {
} }
if (isNestedKeyword(value)) { if (isNestedKeyword(value)) {
return getMappedKeyword(new Keyword((DBObject) value), null); return getMappedKeyword(new Keyword((DBObject) value), documentField.getPropertyEntity());
} }
if (isAssociationConversionNecessary(documentField, value)) { if (isAssociationConversionNecessary(documentField, value)) {

35
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2011-2015 the original author or authors. * Copyright 2011-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -35,6 +35,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Direction;
@ -54,6 +55,7 @@ import org.springframework.data.mongodb.core.mapping.TextScore;
import org.springframework.data.mongodb.core.query.BasicQuery; import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.test.util.BasicDbListBuilder;
import com.mongodb.BasicDBList; import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObject;
@ -68,6 +70,7 @@ import com.mongodb.QueryBuilder;
* @author Patryk Wasik * @author Patryk Wasik
* @author Thomas Darimont * @author Thomas Darimont
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class QueryMapperUnitTests { public class QueryMapperUnitTests {
@ -595,6 +598,28 @@ public class QueryMapperUnitTests {
assertThat(dbo.toString(), equalTo("{ \"embedded\" : { \"$in\" : [ { \"_id\" : \"1\"} , { \"_id\" : \"2\"}]}}")); assertThat(dbo.toString(), equalTo("{ \"embedded\" : { \"$in\" : [ { \"_id\" : \"1\"} , { \"_id\" : \"2\"}]}}"));
} }
/**
* @see DATAMONGO-1406
*/
@Test
public void shouldMapQueryForNestedCustomizedPropertiesUsingConfiguredFieldNames() {
EmbeddedClass embeddedClass = new EmbeddedClass();
embeddedClass.customizedField = "hello";
Foo foo = new Foo();
foo.listOfItems = Arrays.asList(embeddedClass);
Query query = new Query(Criteria.where("listOfItems") //
.elemMatch(new Criteria(). //
andOperator(Criteria.where("customizedField").is(embeddedClass.customizedField))));
DBObject dbo = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Foo.class));
assertThat(dbo, isBsonObject().containing("my_items.$elemMatch.$and",
new BasicDbListBuilder().add(new BasicDBObject("fancy_custom_name", embeddedClass.customizedField)).get()));
}
/** /**
* @see DATAMONGO-647 * @see DATAMONGO-647
*/ */
@ -792,8 +817,7 @@ public class QueryMapperUnitTests {
} }
/** /**
* <<<<<<< HEAD *
*
* @see DATAMONGO-1269 * @see DATAMONGO-1269
*/ */
@Test @Test
@ -859,10 +883,15 @@ public class QueryMapperUnitTests {
public class Foo { public class Foo {
@Id private ObjectId id; @Id private ObjectId id;
EmbeddedClass embedded; EmbeddedClass embedded;
@Field("my_items")
List<EmbeddedClass> listOfItems;
} }
public class EmbeddedClass { public class EmbeddedClass {
public String id; public String id;
@Field("fancy_custom_name") public String customizedField;
} }
class IdWrapper { class IdWrapper {

Loading…
Cancel
Save