1

I'd like to have a Neo4j graph where everything is connected to the reference node (node0). My idea was to connect node0 to a 'class type' node (rootNode) and then have all the nodes of a certain class connected to it. EG:

node0 --> unique RootUser --> many User

I'm using SpringNeo4j so I annotated RootUser and User with @NodeEntity. I have no idea of how to connect node0 to the RootUser in Spring though. I tried to add the following in the RootUser class but it does not work (referenceNode come from neo4jTemplate.getReferenceNode()):

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

What's the best way to achieve this kind of architecture?

Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154

1 Answers1

1

What definitively will work is wiring the reference node to the spring data entities manually:

RelationshipType relationshipType = ...; // Whatever...

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

You could try to declare a class for the reference node:

@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;
    }
}

...and load and set the reference node with:

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

This is untested and I'm not sure if the neo4jTemplate.load() part works.

James
  • 11,654
  • 6
  • 52
  • 81
  • neo4jTemplate.load() does not work: org.neo4j.graphdb.NotFoundException: '__type__' property not found for NodeImpl#0. 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) – Markus Schulte Feb 15 '13 at 14:40