如何在spring数据neo4j中正确编码相同类型节点的层次关系?

Pol*_*off 5 java neo4j spring-data-neo4j

我有一个我想用Neo4j存储的树数据结构.

父节点:CodeSet始终是树的根节点和子节点:Node,子节点本身可以具有相同类型的子节点.它们的关系类型:SUBTREE_OF如下:树数据结构

父节点以红色显示,其本身具有以绿色显示的父节点.

一旦父节点和子节点有一些公共数据,我就创建了一个抽象类:

public abstract class AbstractNode {
    private Long id;
    @NotEmpty
    private String code;
    @Relationship(type = "SUBTREE_OF", direction = Relationship.INCOMING)
    private Set<Node> children;

    <getters & setters omitted>
}
Run Code Online (Sandbox Code Playgroud)

父节点的类:

public class CodeSet extends AbstractNode {
    @Relationship(type = "SUBTREE_OF", direction = Relationship.OUTGOING)
    private Application parent;

    <getters and setters omitted>
}
Run Code Online (Sandbox Code Playgroud)

子节点的类:

public class Node extends AbstractNode {
    @NotEmpty
    private String description;
    @NotEmpty
    private String type;
    @NotEmpty
    private String name;
    @NotNull
    @Relationship(type = "SUBTREE_OF", direction = Relationship.OUTGOING)
    private AbstractNode parent;

    <getters and setters omitted>
}
Run Code Online (Sandbox Code Playgroud)

我需要的只是更新子节点.我在服务层使用以下方法:

public Node update(Node node, Long nodeId) throws EntityNotFoundException {
    Node updated = findById(nodeId, 0);
    updated.setDescription(node.getDescription());
    updated.setType(node.getType());
    updated.setName(node.getName());
    updated.setCode(node.getCode());
    nodeRepository.save(updated);
    return updated;
}
Run Code Online (Sandbox Code Playgroud)

有了这个,我得到了以下结果: 节点更新的结果 这种关系破裂了.我也尝试depth=1findById方法参数中指定,但这又导致了错误的关系: 在此输入图像描述

之后我尝试将我的类中的双向关系修改为单向,因为只有一个类有一个带有注释的@Relatinship字段指向另一个,但这也没有用.

如何使这项工作?

Pol*_*off 5

通过更新服务方法中的保存操作解决:

public Node update(Node node, Long nodeId) throws EntityNotFoundException {
    Node updated = findById(nodeId, 0);
    updated.setDescription(node.getDescription());
    updated.setType(node.getType());
    updated.setName(node.getName());
    updated.setCode(node.getCode());
    //added param depth=0 here
    nodeRepository.save(updated, 0);
    return updated;
}
Run Code Online (Sandbox Code Playgroud)