I've been trying to represent a directed graph using neo4j entities. Working on spring-data-neo4j 4.1.0.BUILD-SNAPSHOT
In my scenario there is a Node which represents a generic graph node, and NodeWithScore which inherits from Node and contains some additional data. All the nodes in the graph are connected to exactly one NodeWithScore.
I've used the following mapping:
@NodeEntity
@Setter
@Getter
@NoArgsConstructor
public class Node {
@GraphId
private Long id;
private String name;
@Relationship(type = "SCORE")
private NodeWithScore nodeWithScore;
@Relationship(type = "CHILD")
private Set<Node> children;
@Override
public String toString() {
return String.format("%s -> %s", name,
children == null ? "[]" : children.stream().map(Node::getName).collect(toList()));
}
}
and
@NodeEntity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class NodeWithScore extends Node {
protected Double score;
}
When calling the following code:
Node a = new Node();
a.setName("A");
Node b = new Node();
b.setName("B");
NodeWithScore c = new NodeWithScore();
c.setName("C");
a.setChildren(ImmutableSet.of(b));
b.setChildren(ImmutableSet.of(c));
a.setNodeWithScore(c);
b.setNodeWithScore(c);
c.setNodeWithScore(c);
session.save(a);
everything works as expected - node A has a single child B, which in turn has a single child C, and all three are connected through a SCORE relationship with node C.
And obviously, if in the next line I call:
ImmutableSet.of(a, b, c).forEach(System.out::println);
the result will be:
A -> [B]
B -> [C]
C -> []
However, if in the very next line I try to call:
session.loadAll(Node.class).forEach(System.out::println);
I'm getting the following result:
A -> [B, C]
B -> [C]
C -> [B]
Please let me know if it's a bug with neo4j, or am I doing something wrong.