在 Spring Data Neo4j 4.1 中建模 tree/hierarchy 结构
Modelling tree/hierarchy structure in Spring Data Neo4j 4.1
为了使用 Spring Data Neo4j 4.1 对 tree/hierarchy(父子关系可以双向遍历)建模,我编写了以下实体 class
@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
关系,以至于只能有一个这样的 incoming节点的关系。也就是说,如以下测试用例所示,一个节点不能有多个子节点 - "conflicting" 关系会自动删除:
@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
}
}
测试设置为使用嵌入式驱动程序。 "breakpoints"处的日志输出如下:
为了降低噪音,我限制自己包含我认为可能与问题相关的日志输出。如果需要,请在评论中索取更多输出。配置等也是一样
断点 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}]}
断点 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}
我怀疑问题与第二行("breakpoint 4")中的警告有关,但我不理解reason/solution。
为什么无法映射字段?为什么这会导致上面显示的语义?您如何正确地为可以双向遍历父子关系的树建模?
附加信息:
如果我删除 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
。
当你有一个传入的@Relationship 时,你必须用类型和方向为 INCOMING 的@Relationship 注释字段、访问器和更改器方法。
其次,我认为 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 and the test 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) 的评论,这是必需的,因为如果没有它,您的 object 模型将与您期望的图形模型不同步。当 OGM 映射这个时,它会发现 child 有一个 parent,但是 parent 不包括 child- 所以它会选择一个或另一个作为真理的来源。
为了使用 Spring Data Neo4j 4.1 对 tree/hierarchy(父子关系可以双向遍历)建模,我编写了以下实体 class
@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
关系,以至于只能有一个这样的 incoming节点的关系。也就是说,如以下测试用例所示,一个节点不能有多个子节点 - "conflicting" 关系会自动删除:
@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
}
}
测试设置为使用嵌入式驱动程序。 "breakpoints"处的日志输出如下:
为了降低噪音,我限制自己包含我认为可能与问题相关的日志输出。如果需要,请在评论中索取更多输出。配置等也是一样
断点 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}]}
断点 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}
我怀疑问题与第二行("breakpoint 4")中的警告有关,但我不理解reason/solution。
为什么无法映射字段?为什么这会导致上面显示的语义?您如何正确地为可以双向遍历父子关系的树建模?
附加信息:
如果我删除 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
。
当你有一个传入的@Relationship 时,你必须用类型和方向为 INCOMING 的@Relationship 注释字段、访问器和更改器方法。
其次,我认为 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 and the test 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) 的评论,这是必需的,因为如果没有它,您的 object 模型将与您期望的图形模型不同步。当 OGM 映射这个时,它会发现 child 有一个 parent,但是 parent 不包括 child- 所以它会选择一个或另一个作为真理的来源。