用Neo4J OGM映射树结构

时间:2019-03-09 13:34:37

标签: java neo4j cypher neo4j-ogm neo4j-embedded

我正在使用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

1 个答案:

答案 0 :(得分:0)

您不需要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关系指向父母而不是孩子。