为了使用Spring Data Neo4j 4.1对树/层次结构(可以双向遍历父子关系)进行建模,我编写了以下实体类
@NodeEntity(label = "node")
public class Node {
@GraphId
@SuppressWarnings("unused")
private Long graphId;
private String name;
@Relationship(type = "PARENT", direction = Relationship.OUTGOING)
private Node parent;
@Relationship(type = "PARENT", direction = Relationship.INCOMING)
private Iterable<Node> children;
@SuppressWarnings("unused")
protected Node() {
// For SDN.
}
public Node(String name, Node parent) {
this.name = Objects.requireNonNull(name);
this.parent = parent;
}
public String getName() {
return name;
}
public Node getParent() {
return parent;
}
}
问题在于,显然children
字段的存在会使PARENT
关系失效,因此节点只能有一个这样的传入关系。也就是说,如下面的测试用例所示,一个节点不能有多个子节点 - “冲突”关系会被自动删除:
@RunWith(SpringRunner.class)
@SpringBootTest(
classes = GraphDomainTestConfig.class,
webEnvironment = SpringBootTest.WebEnvironment.NONE
)
@SuppressWarnings("SpringJavaAutowiredMembersInspection")
public class NodeTest {
@Autowired
private NodeRepository repository;
@Test
public void test() {
// Breakpoint 0
Node A = new Node("A", null);
A = repository.save(A);
// Breakpoint 1
Node B = new Node("B", A);
Node C = new Node("C", A);
B = repository.save(B);
// Breakpoint 2
C = repository.save(C);
// Breakpoint 3
A = repository.findByName("A");
B = repository.findByName("B");
C = repository.findByName("C");
// Breakpoint 4
assertNull(A.getParent()); // OK
assertEquals(B.getParent().getName(), "A"); // FAILS (null pointer exception)!
assertEquals(C.getParent().getName(), "A"); // OK
}
}
测试设置为使用嵌入式驱动程序。 “断点”处的日志输出如下:
为了降低噪音,我限制自己包含我认为可能与问题相关的日志输出。如果需要,请在评论中询问更多输出。配置等也是如此。
断点0 :奇怪的警告。
WARN: No identity field found for class of type: com.example.NodeTest when creating persistent property for field: private com.example.NodeRepository com.example.NodeTest.repository
断点1 :创建了节点A.
INFO: Request: UNWIND {rows} as row CREATE (n:`node`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, row.type as type with params {rows=[{nodeRef=-1965998569, type=node, props={name=A}}]}
断点2 :创建节点B及其与A的关系。
INFO: Request: UNWIND {rows} as row CREATE (n:`node`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, row.type as type with params {rows=[{nodeRef=-1715570484, type=node, props={name=B}}]}
INFO: Request: UNWIND {rows} as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`PARENT`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, row.type as type with params {rows=[{startNodeId=1, relRef=-1978848273, type=rel, endNodeId=0}]}
断点3 :创建节点C及其与A的关系。但B与A的关系也被删除了!
INFO: Request: UNWIND {rows} as row CREATE (n:`node`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, row.type as type with params {rows=[{nodeRef=-215596349, type=node, props={name=C}}]}
INFO: Request: UNWIND {rows} as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`PARENT`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, row.type as type with params {rows=[{startNodeId=2, relRef=-2003500348, type=rel, endNodeId=0}]}
INFO: Request: UNWIND {rows} as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`PARENT`]->(endNode) DELETE rel with params {rows=[{startNodeId=1, endNodeId=0}]}
Breakpoint 4 :查询存储库。
INFO: Request: MATCH (n:`node`) WHERE n.`name` = { `name` } WITH n MATCH p=(n)-[*0..1]-(m) RETURN p, ID(n) with params {name=A}
WARN: Cannot map iterable of class com.example.Node to instance of com.example.Node. More than one potential matching field found.
INFO: Request: MATCH (n:`node`) WHERE n.`name` = { `name` } WITH n MATCH p=(n)-[*0..1]-(m) RETURN p, ID(n) with params {name=B}
INFO: Request: MATCH (n:`node`) WHERE n.`name` = { `name` } WITH n MATCH p=(n)-[*0..1]-(m) RETURN p, ID(n) with params {name=C}
我怀疑问题是连接到第二行(“断点4”)中的警告,但我不明白它的原因/解决方案。
为什么不能映射该字段?为什么这会导致上面显示的语义?你如何正确地建模树,在那里你可以双向遍历父子关系?
其他信息:
如果我删除了children
字段,则测试通过。颠倒关系的方向或使字段类型(或子类型)Collection
没有任何区别。
相关项目依赖项为org.springframework.boot:spring-boot-starter-data-neo4j:jar:1.4.0.RELEASE:compile
,org.neo4j:neo4j-ogm-test:jar:2.0.4:test
和org.neo4j.test:neo4j-harness:jar:3.0.4:test
。
答案 0 :(得分:1)
如果有传入的@Relationship,则必须使用类型和方向为INCOMING的@Relationship注释field,accessor和mutator方法。
其次,我认为Iterable for children不适用于OGM映射过程 - List,Vector,Set,SortedSet的实现。
我们在这里有一个树的示例:https://github.com/neo4j/neo4j-ogm/blob/2.0/core/src/test/java/org/neo4j/ogm/domain/tree/Entity.java和测试https://github.com/neo4j/neo4j-ogm/blob/2.0/core/src/test/java/org/neo4j/ogm/persistence/examples/tree/TreeIntegrationTest.java
修改强>
所以我再次看一下代码 - Iterable可能会起作用。可能实际上是一套。关于你对parent.children.add(this)的评论,它是必需的,因为没有它,你的对象模型与你期望的图模型不同步。当OGM映射时,它可以发现孩子有父母,但父母不包括孩子 - 因此它会选择一个或另一个作为真相的来源。