0

We have a structure where a Parent can have multiple child with nested structure.

1: Parent p1
    child c1
          c1.1       
          c1.2
   child  c2
          c2.1
          c2.3

Now using one cypher query , I need to get the whole structure using Spring + Neo 4j.

Model:

Person:

@Relationship( direction = Relationship.OUTGOING, type = "PARENT")
private Person parent;

@Relationship( direction = Relationship.INCOMING, type = "PARENT")
private List<Person> child;

Cypher Query :-

MATCH (p:Person {name:"john"})<-[pr:PARENT*..2]-(p1:Person) return c1

Gives me only Child but not their next level child

MATCH (p:Person {name:"john"})<-[pr:PARENT*..2]-(p1:Person) return pr

Gives me a nested structure which is recursive which is of no use.

Approach :- repository.findOne(personId, 2);

I am getting the same problem as when we expand the child structure it has one reference of parent Object

For e.g:-

Parent p1 child c1 -- > three Object

  1: child-p1 --- it would have a reference to Parent Object p1
  2: c1.1 ---  
       child --it would reference to Child C1 since its parent    

  3: c1.2
       child --it would reference to Child C1 since its parent

Ideally it should not contain any reference of Parent in Child List and resulting the stack over flow issue.

I am using SDN 4.0.release

1 Answers1

0

One way to do this is load the entity with a custom depth, like this-

repository.findOne(personId, 2);

where 2 is the depth.

If you are using SDN 4.0, your Cypher queries won't be of much use in this case, entities are not mapped from query results.

If you use SDN 4.1, then you can return all nodes and relationships from the path, and your domain entities will be mapped correctly. Example:

MATCH path=(p:Person {name:"john"})<-[pr:PARENT*..2]-(p1:Person) return p as person, nodes(path),rels(path)

If you execute this using Neo4jTemplate.query, you'll receive a org.neo4j.ogm.model.Result which will contain the person, hydrated with relationships from the path you've matched.

Luanne
  • 19,145
  • 1
  • 39
  • 51