Search code examples
spring-data-neo4jspring-data-neo4j-4

Should I expect Neo4jOperations#queryForObjects to work with @QueryResult POJOs?


Neo4jOperations#queryForObjects() doesn't seem to play well with @QueryResult POJOs - it always says that result set is empty.

Trying Neo4jOperations#queryForObjects - it says result is empty:

@Test
public void thisDoesNotWork() {
    Iterable<ClassNodeIdAndName> result = neo4jOperations.queryForObjects(
            ClassNodeIdAndName.class,
            "MATCH (c:ClassNode) RETURN ID(c) AS id, c.name AS name",
            new HashMap<>());
    assertTrue(result.iterator().hasNext());
}

Trying Neo4jOperations#query - says result is NOT empty:

@Test
public void thisWorksFine() {
    Result result = neo4jOperations.query(
            "MATCH (c:ClassNode) RETURN ID(c) AS id, c.name AS name",
            new HashMap<>());
    assertTrue(result.iterator().hasNext());
}

Trying repository with @Query - says result is NOT empty:

@Test
public void thisWorksFineAsWell() {
    List<ClassNodeIdAndName> classNodeIdsAndNames = classNodeRepository.getAllIdsAndNames();
    assertFalse(classNodeIdsAndNames.isEmpty());
}

public interface ClassNodeRepository extends GraphRepository<ClassNode> {
    @Query("MATCH (c:ClassNode) RETURN ID(c) AS id, c.name AS name")
    List<ClassNodeIdAndName> getAllIdsAndNames();
}

@QueryResult
public class ClassNodeIdAndName {
    public Long id;
    public String name;
}

Documentation says that

Iterable queryForObjects(Class entityType,

entityType - The Class denoting the type of entity to return

But I'm confused whether I should look at type of entity or at for objects. If it's not supposed to handle @QueryResult, I would expect it to throw instead of returning no results.

I'm using spring-data-neo4j 4.1.3.RELEASE


Solution

  • @QueryResult is a Spring Data Neo4j concept that applies only to Spring Repositorys.

    Neo4jOperations is a thin wrapper around the Neo4j OGM's Session class and consequently does not handle the concept of returning query result objects.

    Also see: SDN 4 Session.query doesn't work for @QueryResult