diff --git a/morphia/src/main/java/org/mongodb/morphia/mapping/Mapper.java b/morphia/src/main/java/org/mongodb/morphia/mapping/Mapper.java index a85d1dd2190..98bef6d08af 100644 --- a/morphia/src/main/java/org/mongodb/morphia/mapping/Mapper.java +++ b/morphia/src/main/java/org/mongodb/morphia/mapping/Mapper.java @@ -416,7 +416,10 @@ public Object toMongoObject(final MappedField mf, final MappedClass mc, final Ob if (key == null) { mappedValue = toMongoObject(value, false); } else { - mappedValue = keyToRef(key); + final Reference refAnn = mf.getAnnotation(Reference.class); + mappedValue = refAnn != null && refAnn.idOnly() + ? keyToManualRef(key) + : keyToRef(key); if (mappedValue == value) { throw new ValidationException("cannot map to @Reference/Key/DBRef field: " + value); } diff --git a/morphia/src/test/java/org/mongodb/morphia/mapping/ReferencesNotUsingDBRefTest.java b/morphia/src/test/java/org/mongodb/morphia/mapping/ReferencesNotUsingDBRefTest.java index 2289ed48e97..881c1f93352 100644 --- a/morphia/src/test/java/org/mongodb/morphia/mapping/ReferencesNotUsingDBRefTest.java +++ b/morphia/src/test/java/org/mongodb/morphia/mapping/ReferencesNotUsingDBRefTest.java @@ -216,6 +216,18 @@ public void testNullReferences() { getDs().save(container); allNull(container); } + + @Test + public void testFindByEntityReference() { + final Ref ref = new Ref("refId"); + getDs().save(ref); + + final Container container = new Container(); + container.singleRef = ref; + getDs().save(container); + + Assert.assertNotNull(getDs().find(Container.class, "singleRef", ref).get()); + } private void allNull(final Container container) { Assert.assertNull(container.lazyMapRef);