2015-09-26 161 views
0

在我的Spring Boot/Neo4j應用程序中,我有一棵Neo4j節點的大樹,現在我想爲每個節點實現分層註釋。Neo4j分層註釋體系結構和最佳實踐

我計劃創建一個新的Comment實體,現在有幾個關於解決方案設計的問題。

@NodeEntity 
public class Comment { 

    @RelatedTo(type = CONTAINS, direction = Direction.OUTGOING) 
    private Set<Decision> childComments = new HashSet<>(); 

} 

另外,我打算使用註釋針對不同類型,例如用於PostUserCompany等:所以我打算創造這樣的事情

評論在我的應用程序必須是分層的。

我應該創建一些接口,比如說Commentable,並在PostUser,Company類中實現這個接口嗎?

如果是這樣,我可以在我的Comment類中使用此接口(而不是 @NodeEntity)?例如:

@NodeEntity 
public class Comment { 

    @RelatedTo(type = CONTAINS, direction = Direction.OUTGOING) 
    private Set<Decision> childComments = new HashSet<>(); 

    @RelatedTo(type = CONTAINS, direction = Direction.INCOMING) 
    Commentable owner; 

} 

同樣,如果每條評論都會知道它的主人,如何確定,例如(一定Commentable節點根元素徵求意見樹)某個節點上的第一評論?也許通過評論創建日期?

Neo4j/Cypher/SDN中是否有一種方法通過查詢數據直接獲取評論樹,或者它應該在代碼中實現(在我的應用程序的業務邏輯中)?

回答

1

您可以創建一個抽象基類Commentable,與@NodeEntity註釋它,並把在所有的方法中常見的commentables,即getComments等。 混凝土評論只是延伸可評論,你不需要用@NodeEntity註釋。
第二個問題有點含糊:評論者是否擁有一個單一的根評論,或者他們是否擁有一個根評論列表,他們每個都是評論樹的根? 我想後者,所以我建議將可評論的內容鏈接到根註釋並從它們訪問評論樹。

@NodeEntity 
public abstract class Commentable { 

    @RelatedTo(type = COMMENTABLE_COMMENTS, direction = Direction.BOTH) 
    private Set<Comment> comments = new HashSet<>(); 

} 

@NodeEntity 
public class Comment { 

    @RelatedTo(type = CHILD_COMMENTS, direction = Direction.OUTGOING) 
    private Set<Comment> childComments = new HashSet<>(); 

    @RelatedTo(type = COMMENTABLE_COMMENTS, direction = Direction.BOTH) 
    Commentable owner; 

} 

你可以像查詢獲取給定的註釋樹下面:

MATCH (comment {key: {value}})-[:CHILD_COMMENTS*]->(child) 

有我假設你已經獲取根評論和意見有一些關鍵屬性唯一標識他們。

+0

謝謝您的回答。你的假設是正確的 - 「可評論」擁有一個根評論列表。那麼,如何獲取所有評論樹(而不是一棵樹)對於這個特定的可評論?例如,基於'commentable.id'進行查詢? – alexanoid

+0

是的,可以是'MATCH(commentable:{id:{id})< - [:COMMENTABLE_COMMENTS] - >(comment) - [:CHILD_COMMENTS *] - >(child)' – remigio

0

基於聖雷米希奧答案我已經改進的方案,一點點:

@NodeEntity 
public abstract class Commentable { 

    @RelatedTo(type = COMMENTED_ON, direction = Direction.INCOMING) 
    private Set<Comment> comments = new HashSet<>(); 

} 

@NodeEntity 
public class Comment extends Commentable { 

    @RelatedTo(type = COMMENTED_ON, direction = Direction.OUTGOING) 
    private Commentable commentable; 

    private String text; 
}