如何避免 spring 数据 neo4j 获取父节点作为树数据结构中子集合的成员?

How to avoid spring data neo4j fetching parent nodes as a members of a child collection in a tree data structure?

数据模型:

我在 neo4j 中存储了一个树结构,其中 :Node 类型的节点可以是相同类型节点的父节点。

:Node个节点显示在右侧。树的根(显示为红色)与叶子共享一些属性,因此有一个抽象 class 称为 AbstractNode:

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>
}

Class为父节点:

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

    <getters and setters omitted>
}

Class为子节点:

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>
}

服务层:

此方法用于检索指定深度的节点信息:

@Transactional(readOnly = true)
    public Node findById(Long id, int depth) throws EntityNotFoundException {
        Node entity = nodeRepository.findOne(id, depth);
        if (entity == null) {
        throw new EntityNotFoundException(String.format("Node %d not found", id));
        } else {
            return entity;
        }
    }

问题: 当获取 :Node 节点时,具有相同类型父节点的节点在子节点列表中具有这些父节点,这显然是错误的并导致其他问题。请参阅所描述数据集的调试器屏幕截图:

如何解决?

With Spring Data Neo4j (SDN) 当存在相同类型的传入和传出关系的组合时,您需要同时注释字段和传入关系字段的setter/getter,否则您将最终映射不正确。

这是在SDN documentation中说的:

The direction attribute on a @Relationship defaults to OUTGOING. Any fields or methods backed by an INCOMING relationship must be explicitly annotated with an INCOMING direction.

neo4j-ogm(spring数据neo4j 4+中使用的映射库)中也创建了一个关于此的issue/feature请求。