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

时间:2018-06-13 16:18:27

标签: neo4j spring-data spring-data-rest spring-data-neo4j

我开始在Spring Data Rest中使用Neo4J。我有一个节点实体和一个用于建模节点和边缘的关系实体。我可以使用postman创建新的节点。

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

我不确定JSON格式在节点之间创建关系是什么。例如:

  1. 创建新节点并与现有节点相关
  2. 在两个现有节点之间创建关系。
  3. 我非常感谢任何关于我需要使用什么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;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

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

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

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

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

希望这有助于某人。

相关问题