簡體   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