2016-03-02 66 views
0

我們有一個Parent可以有多個嵌套結構的子結構。Neo4j - Parent - Child heirarchy + Spring

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

現在使用一個密碼查詢,我需要使用Spring + Neo 4j來獲取整個結構。

型號:

人:

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

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

的Cypher查詢: -

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

給我唯一的孩子,但不是他們的下一級子

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

給了我一個嵌套結構這是遞歸的,這是沒用的。

方法: - repository.findOne(personId,2);

我收到了同樣的問題,當我們擴展它的父對象

的一個參考EG的子結構: -

家長P1孩子C1 - >三個對象

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 

理想情況下,它不應該包含子列表中的Parent的任何引用並導致堆棧溢出問題。

我使用SDN 4.0.release做到這一點

回答

0

一種方法是加載實體與自定義的深度,像這個 -

repository.findOne(personId, 2); 

其中2爲深度。

如果您使用的是SDN 4.0,您的Cypher查詢在這種情況下不會有太大的用處,實體不會從查詢結果中映射出來。

如果您使用SDN 4.1,那麼您可以返回路徑中的所有節點和關係,並且您的域實體將被正確映射。 例子:

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

如果執行此使用Neo4jTemplate.query,您會收到一個org.neo4j.ogm.model.Result其中將包含人,與你匹配的路徑關係水合。

相關問題