【问题标题】:Spring Data Neo4j - How to connect a NodeEntity to the reference node 0?Spring Data Neo4j - 如何将 NodeEntity 连接到参考节点 0?
【发布时间】:2012-11-15 16:07:43
【问题描述】:

我想要一个 Neo4j 图表,其中所有内容都连接到参考节点 (node0)。我的想法是将node0连接到“类类型”节点(rootNode),然后将某个类的所有节点连接到它。例如:

node0 --> unique RootUser --> many User

我使用的是 SpringNeo4j,所以我用 @NodeEntity 注释了 RootUser 和 User。我不知道如何在 Spring 中将 node0 连接到 RootUser。我尝试在 RootUser 类中添加以下内容,但它不起作用(referenceNode 来自neo4jTemplate.getReferenceNode()):

@RelatedTo(type = "partition", direction = Direction.INCOMING)
    private Node referenceNode;

实现这种架构的最佳方式是什么?

【问题讨论】:

    标签: neo4j spring-data-neo4j spring-data-graph


    【解决方案1】:

    最终会起作用的是将参考节点手动连接到弹簧数据实体:

    RelationshipType relationshipType = ...; // Whatever...
    
    RootUser rootUser = new RootUser();
    rootUser.persist();
    neo4jTemplate.getReferenceNode().createRelationshipTo(rootUser.getPersistentState(), relationshipType);
    

    您可以尝试为引用节点声明一个类:

    @NodeEntity
    public class ReferenceNode {
    }
    
    @NodeEntity
    public class RootUser {
        @RelatedTo(type = "partition", direction = Direction.INCOMING)
        private ReferenceNode referenceNode;
    
        public void setReferenceNode(ReferenceNode referenceNode) {
            this.referenceNode = root;
        }
    }
    

    ...并加载并设置参考节点:

    ReferenceNode referenceNode = neo4jTemplate.load(neo4jTemplate.getReferenceNode(), ReferenceNode.class);
    RootUser rootUser = new RootUser();
    rootUser.persist();
    rootUser.setReferenceNode(referenceNode);
    

    这是未经测试的,我不确定neo4jTemplate.load() 部分是否有效。

    【讨论】:

    • neo4jTemplate.load() 不起作用:org.neo4j.graphdb.NotFoundException:找不到 NodeImpl#0 的“type”属性。 at org.neo4j.kernel.impl.core.Primitive.newPropertyNotFoundException(Primitive.java:184) at org.neo4j.kernel.impl.core.Primitive.getProperty(Primitive.java:179) at org.neo4j.kernel.impl .core.NodeImpl.getProperty(NodeImpl.java:52) at org.neo4j.kernel.impl.core.NodeProxy.getProperty(NodeProxy.java:155) at org.springframework.data.neo4j.support.typerepresentation.AbstractIndexingTypeRepresentationStrategy.readAliasFrom (AbstractIndexingTypeRepresentationStrategy.java:107)
    猜你喜欢
    • 2015-07-19
    • 2013-06-08
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    相关资源
    最近更新 更多