java - Spring Data Neo4j 4.1 中的树/层次结构建模

标签 java neo4j spring-data-neo4j-4 neo4j-ogm

为了使用 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}]}

断点 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:compileorg.neo4j:neo4j- ogm-test:jar:2.0.4:testorg.neo4j.test:neo4j-harness:jar:3.0.4:test

最佳答案

当您有传入的 @Relationship 时,您必须使用类型和方向为 INCOMING 的 @Relationship 来注释字段、访问器和修改器方法。

其次,我相信子级的 Iterable 无法与 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 映射这一点时,它可能会发现 child 有一个 parent ,但 parent 不包括 child - 因此它会选择一个或另一个作为事实来源。

关于java - Spring Data Neo4j 4.1 中的树/层次结构建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39389626/

相关文章:

java - Spring-data-neo4j 4.0.0 存储库进行多个 REST 调用而不是对它们进行分组

Java 编程方式获取与类关联的二进制数据

java - 使用 Lucene 提取英语单词

nosql - 什么是已知的最大Neo4j集群?

rest - 创建工作正常,但 neo4j post params 中的 MERGE 有错误

neo4j - SDN4 : Recent snapshot build broken

java - 无法在 Kotlin 中创建 Spring Data 事件监听器

Java 找不到符号错误 - 来自另一个类的方法

java - 带有 MouseListener 的 CardLayout 不起作用

java - 如何存储和检索 Neo4j 索引