Search code examples
javaneo4jspring-data-neo4jneo4j-ogm

Relationship entities with depth 1 also fetching Node's relationships


I'm using neo4j-ogm 3.1.5 in my project.

In my code, when I'm fetching any relationship entity with depth = 1, it is fetching startNode and endNode and it is also fetching relationships of startNode and endNode. Basically, it the depth parameter is working as depth = depth + 1, because the same value of depth is passed to the nodes when fetching relationship entity.

AFAIK understand depth parameter is used basically like a hibernate's LAZY or EAGER loading. In SchemaRelationshipLoadClauseBuilder class, its happening in the method

public String build(String variable, String label, int depth)

Steps to Reproduce

Fetch a relationship entity using findById method

Current Implementation

In SchemaRelationshipLoadClauseBuilder, the following method:

public String build(String variable, String label, int depth)
  • calls expand(sb, "n", start, depth) instead of expand(sb, "n", start, depth-1), AND
  • calls expand(sb, "m", end, depth) instead of expand(sb, "m", end, depth-1).

The thing is, this will cause a problem in my project, as the startNode and endNode of the respective relationship entity can have more than 100 000 relationships of the same kind and fetching all those relationships will take up the memory of the machine.

Can anyone explain why is it so?


Solution

  • The reason for this behaviour is not a bug but the nature of a Cypher query. You cannot load a relationship on its own. There have to be start and end nodes to form a correct query.

    The depth will then get applied to both nodes. Of course this is more or less a kind of definition if you already took one "hop" from a relationship to the nodes but this will definitely also question the general depth model in Neo4j-OGM because suddenly every relationship (without touching a node) will count as a hop and hitting the node would be the next one.