[英]How to code the hierarchical relationship to the node of the same type properly in spring data neo4j?
I have a tree data structure I'd like to store using Neo4j. 我想使用Neo4j存储一个树数据结构。
There is a parent node :CodeSet
, which is always the root of the tree and a child nodes :Node
, which themselves can have child nodes of the same type. 有一个父节点:CodeSet
,它始终是树的根,还有一个子节点:Node
,它们本身可以具有相同类型的子节点。 They are connected with relationship of type :SUBTREE_OF
as follows: 它们通过以下类型的关系连接:SUBTREE_OF
:
The parent node is displayed in red and it itself has a parent displayed in green. 父节点显示为红色,并且其本身有一个父节点显示为绿色。
As soon as parent node and child nodes have some common data, I created an abstract 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 for the parent node: 父节点的类:
public class CodeSet extends AbstractNode {
@Relationship(type = "SUBTREE_OF", direction = Relationship.OUTGOING)
private Application parent;
<getters and setters omitted>
}
Class for the child node: 子节点的类:
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>
}
What I need is just making a child node update. 我需要的只是对子节点进行更新。 I use the following method at my service layer: 我在服务层使用以下方法:
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;
}
With this I got the following result: 有了这个我得到以下结果: The relationship is broken. 关系破裂了。 I also tried out to specify depth=1
at findById
method parameter, but that resulted in wrong relationships once again: 我还尝试在findById
方法参数中指定depth=1
,但这又导致错误的关系:
After that I tried out modifying bi-directional relationship in my classes to uni-directional so as only one class has an annotated with @Relatinship
field pointing to another, but that did not help either. 之后,我尝试将类中的双向关系修改为单向性,这样只有一个类具有带@Relatinship
字段的注释,该注释指向另一个,但是这也无济于事。
How to make this work? 如何使这项工作?
Was resolved by updating the save operation in the service method: 通过更新服务方法中的保存操作来解决:
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;
}
Maybe there is a problem with your definition of relationship in abstract class. 您在抽象类中对关系的定义可能存在问题。 Children's nodes inherit INCOMING relationships too, so when you update using depth 1 relationships are bilateral. 子节点也继承INCOMING关系,因此当您使用深度1更新时,关系是双向的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.