[英]Mapping a tree structure with Neo4J OGM
我正在使用Neo4J OGM(最新版本)將我的數據序列化為Neo4J嵌入式數據庫。
這適用於我的大多數實體,但是一旦我嘗試保存樹狀結構,它需要永遠保存並且似乎創建了數千個這樣的關系,雖然我只是保存一些節點:
RequestExecutor: 223 - creating new node id: -32715, 14690, [255, 100, 139, 207]
RequestExecutor: 223 - creating new node id: -32718, 14691, [29, 95]
RequestExecutor: 223 - creating new node id: -32721, 14692, [255, 102, 142, 212]
RequestExecutor: 223 - creating new node id: -32724, 14693, [30, 95]
RequestExecutor: 223 - creating new node id: -32727, 14694, [255, 103, 143, 213]
RequestExecutor: 223 - creating new node id: -32730, 14695, [31, 95]
RequestExecutor: 223 - creating new node id: -32733, 14696, [255, 103, 143, 213]
RequestExecutor: 223 - creating new node id: -32736, 14697, [32, 95]
這些展開操作也需要很長時間(此行更長,只是一個摘錄):
EmbeddedRequest: 152 - Request: UNWIND {rows} as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`hasParentNd`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, {type} as type with params {type=rel, rows=[{startNodeId=33, relRef=-32770, endNodeId=32, props={}}, {startNodeId=34, relRef=-32773, endNodeId=61, props={}}, {startNodeId=35, relRef=-32776, endNodeId=34, props={}}, {startNodeId=36, relRef=-32779, endNodeId=61, props={}}, {startNodeId=37, relRef=-32782, endNodeId=36, props={}}, {startNodeId=38, relRef=-32785, endNodeId=61, props={}}, {startNodeId=39, relRef=-19, endNodeId=14698, props={}}, {startNodeId=40, relRef=-32788, endNodeId=38, props={}}, {startNodeId=41, relRef=-22, endNodeId=39, props={}}, {startNodeId=42, relRef=-32791, endNodeId=61, props={}}, {startNodeId=43, relRef=-25, endNodeId=41,......
節點本身看起來像這樣。 NeoEntity類擁有唯一的ID。
@NodeEntity
public class NeoNode extends NeoEntity implements Node, Node.Op {
@Property("unique")
private boolean unique = true;
@Relationship(type = "hasChildrenNd", direction = Relationship.INCOMING)
private ArrayList<NeoNode.Op> children = new ArrayList<>();
@Transient
private NeoArtifact.Op<?> artifact;
@Relationship(type = "hasParentNd")
private Op parent;
public NeoNode() {}
...
}
我嘗試過各種各樣的關系,但還沒有找到解決方案。 對任何想法都會非常感激。
附加信息:如果我讓它運行,它會填滿堆,直到它崩潰:
Exception in thread "neo4j.Scheduler-1" Exception in thread "Neo4j UDC Timer" java.lang.OutOfMemoryError: Java heap space
您不需要hasParentNd
關系類型,因為所有neo4j關系都可以雙向導航。 您應該重新使用hasChildrenNd
關系與相反的方向性。 這將允許雙向導航相同的關系實例(而不是創建新的冗余關系)。
嘗試將代碼更改為:
@NodeEntity
public class NeoNode extends NeoEntity implements Node, Node.Op {
@Property("unique")
private boolean unique = true;
@Relationship(type = "hasChildrenNd", direction = Relationship.INCOMING)
private ArrayList<Op> children = new ArrayList<>();
@Transient
private NeoArtifact.Op<?> artifact;
@Relationship(type = "hasChildrenNd", direction = Relationship.OUTGOING)
private Op parent;
public NeoNode() {}
...
}
旁白: hasChildrenNd
類型名稱似乎令人困惑,因為傳出的hasChildrenNd
關系指向父級而不是子級。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.