diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java index 2e2d22da7..e30f0fa16 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java @@ -2464,6 +2464,33 @@ public class MongoTemplateTests { assertThat(result.models.get(0).value(), is("mongodb")); } + /** + * @see DATAMONGO-773 + */ + @Test + public void testShouldSupportQueryWithIncludedDbRefField() { + + Sample sample = new Sample("47111", "foo"); + template.save(sample); + + DocumentWithDBRefCollection doc = new DocumentWithDBRefCollection(); + doc.id = "4711"; + doc.dbRefProperty = sample; + + template.save(doc); + + Query qry = query(where("id").is(doc.id)); + qry.fields().include("dbRefProperty"); + + List result = template.find(qry, DocumentWithDBRefCollection.class); + + assertThat(result, is(notNullValue())); + assertThat(result, hasSize(1)); + assertThat(result.get(0), is(notNullValue())); + assertThat(result.get(0).dbRefProperty, is(notNullValue())); + assertThat(result.get(0).dbRefProperty.field, is(sample.field)); + } + static class DocumentWithDBRefCollection { @Id public String id; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java index 8cad514d4..c30707fac 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java @@ -553,6 +553,21 @@ public class QueryMapperUnitTests { assertThat(inObject.get(0), is(instanceOf(com.mongodb.DBRef.class))); } + /** + * @see DATAMONGO-773 + */ + @Test + public void queryMapperShouldBeAbleToProcessQueriesThatIncludeDbRefFields() { + + BasicMongoPersistentEntity persistentEntity = context.getPersistentEntity(WithDBRef.class); + + Query qry = query(where("someString").is("abc")); + qry.fields().include("reference"); + + DBObject mappedFields = mapper.getMappedObject(qry.getFieldsObject(), persistentEntity); + assertThat(mappedFields, is(notNullValue())); + } + class IdWrapper { Object id; }