【问题标题】:How to avoid spring data neo4j fetching parent nodes as a members of a child collection in a tree data structure?如何避免spring data neo4j在树数据结构中将父节点作为子集合的成员获取?
【发布时间】:2016-10-27 21:52:18
【问题描述】:

数据模型:

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

:Node 节点显示在右侧。树的根(显示为红色)与叶子共享一些属性,因此有一个抽象类称为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>
}

父节点的类:

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

    <getters and setters omitted>
}

子节点的类:

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

如何解决?

【问题讨论】:

  • 您使用的是什么版本的SDN和OGM?
  • @digx1 org.springframework.data:spring-data-neo4j:4.1.2.RELEASE 与对应的org.neo4j:neo4j-ogm-core:2.0.3

标签: java neo4j spring-data-neo4j neo4j-ogm nosql


【解决方案1】:

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

这是SDN documentation里说的:

@Relationship 的方向属性默认为 OUTGOING。任何由 INCOMING 关系支持的字段或方法都必须使用 INCOMING 方向显式注释。

neo4j-ogm(spring data neo4j 4+ 中使用的映射库)中还创建了一个关于此的问题/功能请求。

【讨论】:

  • 那么在当前的 3.0RC1 版本中,对字段进行注释就足够了吗?
  • 是的,@Relationship 注释是仅从 3.x 开始的字段
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多