Skip to content

Commit

Permalink
Throw exception if class cannot get mapped because it was not scanned.
Browse files Browse the repository at this point in the history
  • Loading branch information
meistermeier committed Dec 20, 2018
1 parent e2ee13e commit 86f01ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,6 +52,7 @@
* @author Vince Bickers
* @author Luanne Misquitta
* @author Jasper Blues
* @author Gerrit Meier
*/
public class ExecuteQueriesDelegate extends SessionDelegate {

Expand All @@ -73,7 +75,21 @@ public <T> T queryForObject(Class<T> type, String cypher, Map<String, ?> 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<String, ?> parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -40,6 +43,7 @@

/**
* @author Luanne Misquitta
* @author Gerrit Meier
*/
public class QueryCapabilityTest extends MultiDriverTestClass {

Expand Down Expand Up @@ -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<String, Object> result, boolean foundMichal) {
if (result.get("n") instanceof User) {
User u = (User) result.get("n");
Expand Down

0 comments on commit 86f01ba

Please sign in to comment.