使用 Spring Data Rest 和 neo4j 创建节点之间的关系

Creating relationships between nodes using Spring Data Rest and neo4j

我开始将 Neo4J 与 Spring Data Rest 一起使用。我有一个节点实体和一个用于建模节点和边的关系实体。我可以使用邮递员创建新节点。

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

我不确定 JSON 格式将用于创建节点之间的关系。例如:

  1. 创建新节点并关联现有节点
  2. 在两个现有节点之间创建关系。

任何关于 JSON 我需要使用的例子都将不胜感激。

我的节点实体和关系实体如下:

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

好的,我解决了,你可以这样做:

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

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

这将添加节点之间的关系。

希望这对某人有所帮助。