[英]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.