繁体   English   中英

在Spring Data Neo4j 4.1中建模树/层次结构

[英]Modelling tree/hierarchy structure in Spring Data Neo4j 4.1

为了使用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关系变得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对此进行映射时,它可能会发现孩子有父母,但是父母不包括孩子,因此它将选择一个或另一个作为真理的来源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM