cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Spring Data Neo4j 6: findAll() operation doesn't map relationships properly

(same question was posted on StackOverflow 7 days ago: https://stackoverflow.com/questions/64859925/spring-data-neo4j-6-findall-operation-doesnt-map-relati...)

In Spring Data Neo4j 6 (6.0.1), a basic Neo4jTemplate findAll() operation with a simple relationship doesn't seem to map the relationship entity and its target even though they are part of the result set. Is this a bug or am I missing something?

Let's consider the following basic scenario:

var a = new EntityA();
var b = new EntityB();
a.entityB = b;

neo4jTemplate.save(a);

with

@Node
public class EntityA {
    @Id @GeneratedValue(UUIDStringGenerator.class)
    public String id;

    @Relationship("HAS_ENTITY_B")
    public EntityB entityB;
}

@Node
public class EntityB {
    @Id @GeneratedValue(UUIDStringGenerator.class)
    public String id;
}

When trying to map a result like this:

var result = neo4jTemplate.findAll("MATCH (a:EntityA)-[r:HAS_ENTITY_B]->(b:EntityB) RETURN a,r,b", EntityA.class);
Assert.notNull(result.get(0).entityB, "entityB should not be null here!");

I would expect the entityB property not to be null.

1 ACCEPTED SOLUTION

The return part has to define the relationships and target nodes as lists.
So your query would be MATCH (a:EntityA)-[r:HAS_ENTITY_B]->(b:EntityB) RETURN a, collect(r), collect(b).
This is due to the fact, that SDN 6 expects to get the results for every EntityA within one record. If there would be more than one relationship (and/or EntityB related) Neo4j would return multiple records for the very same EntityA. This cannot get mapped a) in an immutable way and b) keep the reactive flow (if wanted) alive.

View solution in original post

2 REPLIES 2

The return part has to define the relationships and target nodes as lists.
So your query would be MATCH (a:EntityA)-[r:HAS_ENTITY_B]->(b:EntityB) RETURN a, collect(r), collect(b).
This is due to the fact, that SDN 6 expects to get the results for every EntityA within one record. If there would be more than one relationship (and/or EntityB related) Neo4j would return multiple records for the very same EntityA. This cannot get mapped a) in an immutable way and b) keep the reactive flow (if wanted) alive.

Thank you Gerrit. With this modified query, it now works fine. Thanks for the info.