1

我开始将 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;
    }
}
4

1 回答 1

0

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

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

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

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

希望这可以帮助某人。

于 2018-06-15T20:23:02.800 回答