diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java index 2e1e46f9e..2578630e8 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java @@ -553,10 +553,10 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext o = x.getValue(ctx); } else { DBObject from = dbo; - if (dbo instanceof DBRef) { - from = ((DBRef) dbo).fetch(); + Object dbObj = dbo.get(name); + if (dbObj instanceof DBRef) { + dbObj = ((DBRef) dbObj).fetch(); } - Object dbObj = from.get(name); if (dbObj instanceof DBObject) { if (prop.isMap() && dbObj instanceof DBObject) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java index 76859c95a..8af8e637c 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java @@ -56,6 +56,7 @@ public class MappingTests { "personcustomidname", "personmultidimarrays", "personmulticollection", + "personwithdbref", "person1", "person2", "account" @@ -299,4 +300,16 @@ public class MappingTests { assertThat(result.get(0).getGrid().size(), is(1)); } + @Test + public void testDbRef() { + GeoLocation geo = new GeoLocation(new double[]{37.0625, -95.677068}); + template.insert(geo); + + PersonWithDbRef p = new PersonWithDbRef(4321, "With", "DBRef", geo); + template.insert(p); + + List result = template.find(new Query(Criteria.where("ssn").is(4321)), PersonWithDbRef.class); + assertThat(result.size(), is(1)); + } + } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonWithDbRef.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonWithDbRef.java new file mode 100644 index 000000000..1ed41e3a7 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonWithDbRef.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2011 by the original author(s). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.document.mongodb.mapping; + +/** + * @author Jon Brisbin + */ +public class PersonWithDbRef extends BasePerson { + + @DBRef + private GeoLocation home; + + public PersonWithDbRef(Integer ssn, String firstName, String lastName, GeoLocation home) { + super(ssn, firstName, lastName); + this.home = home; + } + + public GeoLocation getHome() { + return home; + } + + public void setHome(GeoLocation home) { + this.home = home; + } +}