0
votes

I am starting to use Neo4J with Spring Data Rest. I have a node entity and a relationship entity for modelling nodes and edges. I'm able to create new nodes with the following using postman.

POST http://localhost:8080/nodes
{  
    "name" : "Test"
}

I am unsure of what the JSON format would be to create relationships between the nodes. For example:

  1. Create a new node and relate to an existing node
  2. Create a relationship between two existing nodes.

Any examples on what JSON I need to use would be very much appreciated.

My node entity and relationship entity are as follows:

@NodeEntity
public class Node {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private int count;

    @Relationship(type = Edge.TYPE, direction = Relationship.UNDIRECTED)
    private Set<Edge> edges = new HashSet<>();

    public void addEdge(Node target, int count) {
        this.edges.add(new Edge(this, target, count));
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Set<Edge> getEdges() {
        return edges;
    }

    public void setEdges(Set<Edge> edges) {
        this.edges = edges;
    }
}

@RelationshipEntity(type = Edge.TYPE)
public class Edge {

    public static final String TYPE = "LINKED_TO";

    @Id
    @GeneratedValue
    private Long relationshipId;

    @StartNode
    private Node start;

    @EndNode
    private Node end;

    private int count;

    public Edge() {
        super();
    }

    public Edge(Node start, Node end, int count) {
        this.start = start;
        this.end = end;
        this.count = count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Node getStart() {
        return start;
    }

    public void setStart(Node start) {
        this.start = start;
    }

    public Node getEnd() {
        return end;
    }

    public void setEnd(Node end) {
        this.end = end;
    }

    public Long getRelationshipId() {
        return relationshipId;
    }

    public void setRelationshipId(Long relationshipId) {
        this.relationshipId = relationshipId;
    }
}
1

1 Answers

0
votes

OK I worked it out, you can do this:

PATCH http://localhost:8080/nodes/1

{  
    "name" : "Test",
    "edges": [
        {
            "start": "http://localhost:8080/nodes/1",
            "end": "http://localhost:8080/nodes/2"
        }
    ]
}

This will add the relationships between the nodes.

Hope this helps someone.