diff --git a/core/src/main/java/org/neo4j/ogm/session/delegates/ExecuteQueriesDelegate.java b/core/src/main/java/org/neo4j/ogm/session/delegates/ExecuteQueriesDelegate.java index cedc7ddfaf..cf90bf029c 100644 --- a/core/src/main/java/org/neo4j/ogm/session/delegates/ExecuteQueriesDelegate.java +++ b/core/src/main/java/org/neo4j/ogm/session/delegates/ExecuteQueriesDelegate.java @@ -30,6 +30,7 @@ import org.neo4j.ogm.cypher.query.DefaultGraphModelRequest; import org.neo4j.ogm.cypher.query.DefaultRestModelRequest; import org.neo4j.ogm.cypher.query.DefaultRowModelRequest; +import org.neo4j.ogm.exception.core.MappingException; import org.neo4j.ogm.metadata.ClassInfo; import org.neo4j.ogm.metadata.FieldInfo; import org.neo4j.ogm.model.GraphModel; @@ -51,6 +52,7 @@ * @author Vince Bickers * @author Luanne Misquitta * @author Jasper Blues + * @author Gerrit Meier */ public class ExecuteQueriesDelegate extends SessionDelegate { @@ -73,7 +75,21 @@ public T queryForObject(Class type, String cypher, Map paramet throw new RuntimeException("Result not of expected size. Expected 1 row but found " + resultSize); } - return results.iterator().next(); + T next = results.iterator().next(); + + if (!next.getClass().isAssignableFrom(type)) { + + String typeOfResult = next.getClass().getName(); + String wantedType = type.getName(); + String message = String.format( + "Cannot map %s to %s. This can be caused by missing registration of %s.", + typeOfResult, wantedType, wantedType + ); + + throw new MappingException(message); + } + + return next; } public Result query(String cypher, Map parameters) { diff --git a/test/src/test/java/org/neo4j/ogm/persistence/session/capability/QueryCapabilityTest.java b/test/src/test/java/org/neo4j/ogm/persistence/session/capability/QueryCapabilityTest.java index 1a58aee37e..b9d7a76e65 100644 --- a/test/src/test/java/org/neo4j/ogm/persistence/session/capability/QueryCapabilityTest.java +++ b/test/src/test/java/org/neo4j/ogm/persistence/session/capability/QueryCapabilityTest.java @@ -13,6 +13,7 @@ package org.neo4j.ogm.persistence.session.capability; +import static java.util.Collections.*; import static org.assertj.core.api.Assertions.*; import java.io.IOException; @@ -30,6 +31,8 @@ import org.neo4j.ogm.domain.cineasts.annotated.Movie; import org.neo4j.ogm.domain.cineasts.annotated.Rating; import org.neo4j.ogm.domain.cineasts.annotated.User; +import org.neo4j.ogm.domain.restaurant.Restaurant; +import org.neo4j.ogm.exception.core.MappingException; import org.neo4j.ogm.model.Result; import org.neo4j.ogm.response.model.NodeModel; import org.neo4j.ogm.session.Session; @@ -40,6 +43,7 @@ /** * @author Luanne Misquitta + * @author Gerrit Meier */ public class QueryCapabilityTest extends MultiDriverTestClass { @@ -710,6 +714,11 @@ public void shouldMapCypherCollectionsToArrays() { assertThat(((Object[]) row.get("names")).length).isEqualTo(0); } + @Test(expected = MappingException.class) + public void shouldThrowExceptionIfTypeMismatchesInQueryForObject() { + session.queryForObject(Restaurant.class, "MATCH (n:User) return count(n)", emptyMap()); + } + private boolean checkForMichal(Map result, boolean foundMichal) { if (result.get("n") instanceof User) { User u = (User) result.get("n");