Spring数据REST Neo4j创建关系
我正在构建一个小的测试应用程序,作为一种学习Angular并在Spring stack上刷新自己的方法。我对Neo4J有一些小的经验,但应用程序的想法是基于一个类似Neo4J的图形数据库 这个想法非常简单,它是一个ui,用于创建角色和故事,并将角色与故事和彼此关联起来,映射其各自的故事版本,并创建一些显示角色交互的图形,以帮助编写总体叙事 我已经很容易地为角色和故事创建了节点,Spring堆栈非常适合为节点本身提供rest易于使用的rest端点。但我找不到任何创建和维护这些节点之间关系的具体例子 例如,在Cypher中,我可以将一个角色与一个故事联系起来,并将人物与故事的关系作为一种关系属性来讲述:Spring数据REST Neo4j创建关系,neo4j,spring-boot,spring-data,spring-data-neo4j,spring-rest,Neo4j,Spring Boot,Spring Data,Spring Data Neo4j,Spring Rest,我正在构建一个小的测试应用程序,作为一种学习Angular并在Spring stack上刷新自己的方法。我对Neo4J有一些小的经验,但应用程序的想法是基于一个类似Neo4J的图形数据库 这个想法非常简单,它是一个ui,用于创建角色和故事,并将角色与故事和彼此关联起来,映射其各自的故事版本,并创建一些显示角色交互的图形,以帮助编写总体叙事 我已经很容易地为角色和故事创建了节点,Spring堆栈非常适合为节点本身提供rest易于使用的rest端点。但我找不到任何创建和维护这些节点之间关系的具体例子
匹配(p:Being),(s:Story),其中id(p)=7,id(s)=16
create(p)-[r:take_PART__{perspective:“我不比一个熟人更了解格雷先生,尽管我认识其他人。他们并不是因为与格雷的关系而变得更好。”}]>(s)return r
然后,通过Spring中的映射,我从REST端点返回的数据为我提供了角色,我可以通过链接获得角色所属的故事。我看不到一种方式,虽然张贴或放置添加或删除故事中的人物
我还在Spring的文档中找到关于节点的具体示例,而不是关于边/关系的示例。有人能提供这样的东西吗
我完全知道Neo4J有它自己的REST接口,这基本上也是Spring所使用的。本练习的主要目的是学习一些新技术(Angular2/typescript)并更新我对Spring stack的知识
谢谢 我不确定是否有其他人找到了一个好的或更好的答案,但以下是我发现的有效方法。我有一个SpringBoot项目正在运行,我将在这个答案中发布一些最相关的代码和示例,但是要查看整个REST服务项目,请检查 因此,这个小应用程序的目的是创建参与故事的角色/生物,创建故事(以标题和情节为特色),并在一个生物和一个故事之间创建一种关系,其中该生物的故事视角附在该关系上。通过这种方式,它从每个角色收集故事的不同版本 neo4j实例只是我的Windows笔记本电脑上Docker/Kitematic中的一个基本neo4j实例。以下是模型: Being.java:
package com.chronicler.model;
import java.util.Iterator;
import java.util.Set;
import org.springframework.data.neo4j.annotation.Fetch;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.RelatedTo;
import org.springframework.data.neo4j.annotation.RelatedToVia;
@NodeEntity
public class Being {
public Long getId() {
return id;
}
@GraphId private Long id;
private String firstName;
private String lastName;
private boolean hero;
private boolean villain;
@RelatedToVia(type="TOOK_PART_IN")
@Fetch private Set<Involvement> involvements;
public Set<Involvement> getInvolvements() {
return involvements;
}
public void setInvolvements(Set<Involvement> involvements) {
this.involvements = involvements;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean isHero() {
return hero;
}
public void setHero(boolean hero) {
this.hero = hero;
}
public boolean isVillain() {
return villain;
}
public void setVillain(boolean villain) {
this.villain = villain;
}
}
java(存在与故事之间的关系)
在此基础上,我为spring数据服务设置了基本类型的存储库rest资源类。他们照顾实体,但他们没有真正为我解决关系。我们要做的是实现一个单独的rest路由来保存它
BeingController.java:
package com.chronicler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.chronicler.model.Involvement;
import com.chronicler.repo.InvolvementRepository;
@RestController
public class BeingController {
@Autowired
InvolvementRepository involvementRepository;
@RequestMapping(value="/beingStory", method=RequestMethod.POST)
public Involvement createBeingStoryRelationship(@RequestBody Involvement involvement) {
involvementRepository.save(involvement);
return involvement;
}
}
从这里开始,只需使用以下类型的json正文发布到localhost:3000/beingStory,就可以准确地创建关系:
{
"character": {
"id": 17,
"firstName": "Dr. Victor",
"lastName": "Frankenstein",
"hero": true,
"villain": true
},
"story": {
"id": 15,
"title": "Frankenstein",
"slug": "A doctor plays god"
},
"relationshipId": 10,
"perspective": "I did a monstrous thing. I wanted to create life, but I created horrors... such unimaginable horrors, such that mankind has not ever imagined."
}
从这一点开始,你可以将人际关系从一个人带到故事。我将不得不在未来添加更多内容,因为我将通过这个示例应用程序执行更多内容,以实现查看所选故事中角色参与的反向关系,并且我打算添加角色之间的关系 假设您正在寻找关于创建/修改与SpringDataNeo4j的关系的文档,您看到了吗
package com.chronicler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.chronicler.model.Involvement;
import com.chronicler.repo.InvolvementRepository;
@RestController
public class BeingController {
@Autowired
InvolvementRepository involvementRepository;
@RequestMapping(value="/beingStory", method=RequestMethod.POST)
public Involvement createBeingStoryRelationship(@RequestBody Involvement involvement) {
involvementRepository.save(involvement);
return involvement;
}
}
{
"character": {
"id": 17,
"firstName": "Dr. Victor",
"lastName": "Frankenstein",
"hero": true,
"villain": true
},
"story": {
"id": 15,
"title": "Frankenstein",
"slug": "A doctor plays god"
},
"relationshipId": 10,
"perspective": "I did a monstrous thing. I wanted to create life, but I created horrors... such unimaginable horrors, such that mankind has not ever imagined."
}