如何在 spring data neo4j 中正确编码与同一类型节点的层次关系?
How to code the hierarchical relationship to the node of the same type properly in spring data neo4j?
我有一个树数据结构,我想使用 Neo4j 进行存储。
有一个父节点:CodeSet
,它总是树的根和一个子节点:Node
,它们本身可以有相同类型的子节点。它们与 :SUBTREE_OF
类型的关系连接如下:
父节点显示为红色,它本身有一个父节点显示为绿色。
只要父节点和子节点有一些共同的数据,我就创建了一个抽象class:
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>
}
我需要的只是更新子节点。我在我的服务层使用以下方法:
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;
}
有了这个,我得到了以下结果:
关系破裂了。我还尝试在 findById
方法参数中指定 depth=1
,但这再次导致了错误的关系:
之后,我尝试将 classes 中的双向关系修改为单向关系,这样只有一个 class 有一个带注释的 @Relatinship
字段指向另一个,但这也无济于事。
如何进行这项工作?
已通过更新服务方法中的保存操作解决:
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;
}
可能你对抽象关系的定义有问题class。子节点也继承 INCOMING 关系,因此当您使用深度 1 更新时,关系是双边的。
我有一个树数据结构,我想使用 Neo4j 进行存储。
有一个父节点:CodeSet
,它总是树的根和一个子节点:Node
,它们本身可以有相同类型的子节点。它们与 :SUBTREE_OF
类型的关系连接如下:
父节点显示为红色,它本身有一个父节点显示为绿色。
只要父节点和子节点有一些共同的数据,我就创建了一个抽象class:
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>
}
我需要的只是更新子节点。我在我的服务层使用以下方法:
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;
}
有了这个,我得到了以下结果:
findById
方法参数中指定 depth=1
,但这再次导致了错误的关系:
之后,我尝试将 classes 中的双向关系修改为单向关系,这样只有一个 class 有一个带注释的 @Relatinship
字段指向另一个,但这也无济于事。
如何进行这项工作?
已通过更新服务方法中的保存操作解决:
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;
}
可能你对抽象关系的定义有问题class。子节点也继承 INCOMING 关系,因此当您使用深度 1 更新时,关系是双边的。