Neo4j OGM 和与子类的关系

Neo4j OGM and relations with subclasses

运行 与 Neo4j OGM 库有关,并且与 "subclasses":

有关系
@NodeEntity class MyEntity{
    @GraphId private Long graphId;
    ...
}

class MyRoot extends MyEntity{
    MyResource resource;
    ...
}

class MyResource extends MyEntity{
    @Relationship(type="HAS_CHILD", direction = Relationship.INCOMING)
    private MyContainer parent;
    ...
}

class MyContainer extends MyResource{
    @Relationship(type="HAS_CHILD", direction = Relationship.OUTGOING)
    private List<MyResource> children = new ArrayList<>();
    ...
}

像这样保存一个简单的图表,

我无法取回 children,而调试日志显示 "More than one class subclasses org.springdot.ogm.eval.entities.MyEntity"。

graph to be saved: r=MyRoot{children=[MyResource{graphId=null, name='.1'}, MyResource{graphId=null, name='.2'}, MyResource{graphId=null, name='.3'}, children=[MyResource{graphId=null, name='.4.1'}, MyResource{graphId=null, name='.4.2'}, MyResource{graphId=null, name='.4.3'}]]}
...
16:52:16.880 [main] DEBUG org.neo4j.ogm.metadata.MetaData - More than one class subclasses org.springdot.ogm.eval.entities.MyEntity
16:52:16.881 [main] DEBUG org.neo4j.ogm.metadata.MetaData - More than one class subclasses org.springdot.ogm.eval.entities.MyEntity
...
graph read back: MyRoot{children=[]}

显示问题的完整示例项目在 Github

该调试信息仅表示找到了多个子类,但这不是错误情况。

你没有得到整个结构的原因是因为这行代码

Collection<MyRoot> roots = session.loadAll(MyRoot.class);

default loading depth 加载 MyRoot,即 1

这将加载 MyRoot,设置其属性,并且还将加载相关实体,在本例中为 "container" 但不加载其相关实体。

如果您增加加载深度(在您的情况下,我将其设置为 3),您应该会看到按预期获取的图表。

Collection<MyRoot> roots = session.loadAll(MyRoot.class,3);