Repository: morphiaorg/morphia
Issue: Incorrect validation exception messages in QueryValidator

In QueryValidator's validateQuery method, when a mapped field is not found for a given part, the exception message refers to the original class being mapped rather than the class being used to look up the field:

```
if (mf == null) {                                                                                              
    throw new ValidationException(format("The field '%s' could not be found in '%s' while validating - %s; if "
                                         + "you wish to continue please disable validation.", part,            
                                         clazz.getName(), prop                                                 
                                        ));                                                                    
}                                                                                                              
```

Further along, when checking for queries into `@Reference` or `@Serialized` fields, the exception message is:

```
format("Can not use dot-notation past '%s' could not be found in '%s' while"
       + " validating - %s", part, clazz.getName(), prop))
```

which looks like a copy-paste typo. 

I have a branch off of master on my fork which fixes these issues; should I create a pull request?


Changed files:
morphia/src/main/java/org/mongodb/morphia/query/QueryValidator.java, morphia/src/test/java/org/mongodb/morphia/TestQuery.java, morphia/src/test/java/org/mongodb/morphia/query/QueryValidatorTest.java

diff --git a/morphia/src/main/java/org/mongodb/morphia/query/QueryValidator.java b/morphia/src/main/java/org/mongodb/morphia/query/QueryValidator.java
index df732969bef..ba17e02d818 100644
--- a/morphia/src/main/java/org/mongodb/morphia/query/QueryValidator.java
+++ b/morphia/src/main/java/org/mongodb/morphia/query/QueryValidator.java
@@ -67,7 +67,7 @@ final class QueryValidator {
                     if (mf == null) {
                         throw new ValidationException(format("The field '%s' could not be found in '%s' while validating - %s; if "
                                                              + "you wish to continue please disable validation.", part,
-                                                             clazz.getName(), prop
+                                                             mc.getClazz().getName(), prop
                                                             ));
                     }
                     hasTranslations = true;
@@ -87,8 +87,8 @@ final class QueryValidator {
                 if (!fieldIsArrayOperator) {
                     //catch people trying to search/update into @Reference/@Serialized fields
                     if (!canQueryPast(mf)) {
-                        throw new ValidationException(format("Can not use dot-notation past '%s' could not be found in '%s' while"
-                                                             + " validating - %s", part, clazz.getName(), prop));
+                        throw new ValidationException(format("Cannot use dot-notation past '%s' in '%s'; found while"
+                                                             + " validating - %s", part, mc.getClazz().getName(), prop));
                     }
 
                     //get the next MappedClass for the next field validation
diff --git a/morphia/src/test/java/org/mongodb/morphia/TestQuery.java b/morphia/src/test/java/org/mongodb/morphia/TestQuery.java
index eaf15da3490..beca4969eba 100644
--- a/morphia/src/test/java/org/mongodb/morphia/TestQuery.java
+++ b/morphia/src/test/java/org/mongodb/morphia/TestQuery.java
@@ -730,7 +730,7 @@ public class TestQuery extends TestBase {
             getDs().find(ContainsPic.class, "pic.name", "foo").get();
             assertNull("um, query validation should have thrown");
         } catch (ValidationException e) {
-            assertTrue(e.getMessage().contains("Can not use dot-"));
+            assertTrue(e.getMessage().contains("Cannot use dot-"));
         }
     }
 
diff --git a/morphia/src/test/java/org/mongodb/morphia/query/QueryValidatorTest.java b/morphia/src/test/java/org/mongodb/morphia/query/QueryValidatorTest.java
index e7e49cbe45c..28d2ede659a 100644
--- a/morphia/src/test/java/org/mongodb/morphia/query/QueryValidatorTest.java
+++ b/morphia/src/test/java/org/mongodb/morphia/query/QueryValidatorTest.java
@@ -2,8 +2,12 @@ package org.mongodb.morphia.query;
 
 import com.mongodb.BasicDBObject;
 import org.bson.types.ObjectId;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.mongodb.morphia.Key;
+import org.mongodb.morphia.annotations.Reference;
+import org.mongodb.morphia.annotations.Serialized;
 import org.mongodb.morphia.entities.EntityWithListsAndArrays;
 import org.mongodb.morphia.entities.SimpleEntity;
 import org.mongodb.morphia.mapping.MappedClass;
@@ -11,6 +15,7 @@ import org.mongodb.morphia.mapping.MappedField;
 import org.mongodb.morphia.mapping.Mapper;
 import org.mongodb.morphia.query.validation.ValidationFailure;
 
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -31,6 +36,9 @@ import static org.mongodb.morphia.query.FilterOperator.SIZE;
 import static org.mongodb.morphia.query.QueryValidator.validateQuery;
 
 public class QueryValidatorTest {
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
+
     @Test
     public void shouldAllowAllOperatorForIterableMapAndArrayValues() {
         // expect
@@ -459,10 +467,46 @@ public class QueryValidatorTest {
                                                           new ArrayList<ValidationFailure>()), is(false));
     }
 
+    @Test
+    public void shouldReferToMappedClassInExceptionWhenFieldNotFound() {
+        thrown.expect(ValidationException.class);
+        thrown.expectMessage("The field 'notAField' could not be found in 'org.bson.types.ObjectId'");
+        validateQuery(SimpleEntity.class, new Mapper(), new StringBuilder("id.notAField"), FilterOperator.EQUAL, 1, true, true);
+    }
+
+    @Test
+    public void shouldReferToMappedClassInExceptionWhenQueryingPastReferenceField() {
+        thrown.expect(ValidationException.class);
+        thrown.expectMessage("Cannot use dot-notation past 'reference' in 'org.mongodb.morphia.query.QueryValidatorTest$WithReference'");
+        validateQuery(WithReference.class, new Mapper(), new StringBuilder("reference.name"), FilterOperator.EQUAL, "", true, true);
+    }
+
+    @Test
+    public void shouldReferToMappedClassInExceptionWhenQueryingPastSerializedField() {
+        thrown.expect(ValidationException.class);
+        thrown.expectMessage("Cannot use dot-notation past 'serialized' in "
+                             + "'org.mongodb.morphia.query.QueryValidatorTest$WithSerializedField'");
+        validateQuery(WithSerializedField.class, new Mapper(), new StringBuilder("serialized.name"), FilterOperator.EQUAL, "", true, true);
+    }
+
     private static class GeoEntity {
         private final int[] array = {1};
     }
 
     private static class NullClass {
     }
+
+    private static class WithReference {
+        @Reference
+        private SimpleEntity reference;
+    }
+
+    private static class SerializableClass implements Serializable {
+        private String name;
+    }
+
+    private static class WithSerializedField {
+        @Serialized
+        private SerializableClass serialized;
+    }
 }