Spring Data Neo4j - How to connect a NodeEntity to the reference node 0?

503 views Asked by At

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?

1

There are 1 answers

1
James On

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.