[英]Neo4j save @Relationship data
Here's MVCE: https://github.com/neo4j-examples/movies-java-spring-data-neo4j If you change one test to: 这是MVCE: https : //github.com/neo4j-examples/movies-java-spring-data-neo4j如果您将一项测试更改为:
@Test
public void testFindByTitle() {
String title = "The Matrix";
Movie result = movieRepository.findByTitle(title);
Person p = personRepository.findByName("Keanu Reeves");
assertNotNull(result);
assertEquals(1999, result.getReleased());
}
You can see in debug mode that object p
does not have any movies
. 您可以在调试模式下看到对象
p
没有任何movies
。
Person
entity is: Person
实体为:
@NodeEntity
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
private int born;
@Relationship(type = "ACTED_IN")
private List<Movie> movies = new ArrayList<>();
public Person() {
}
public Person(String name, int born) {
this.name = name;
this.born = born;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public int getBorn() {
return born;
}
public List<Movie> getMovies() {
return movies;
}
}
This is offical example from neo4j. 这是neo4j的官方示例。 How can i store entity
Person
with movies
in database and also have Movie
entity with roles
? 如何将带有
movies
实体“ Person
存储在数据库中,又如何使“ Movie
实体具有roles
?
Edit: What i can do is add in Person
entity method: 编辑:我可以做的是在
Person
实体方法中添加:
public void addMovie(Movie movie) {
if (this.movies == null) {
this.movies = new ArrayList<>();
}
this.movies.add(movie);
}
And in the test add: 并在测试中添加:
p.addMovie(matrix);
personRepository.save(p);
But i don't like this - cause i setting it manually from two sites. 但是我不喜欢-因为我从两个站点手动设置它。
You do not need to set the references manually from two sides. 您无需从两侧手动设置引用。 Expand your code snippet slightly by a single line
movie.setPerson(this);
通过单行
movie.setPerson(this);
稍微扩展您的代码段movie.setPerson(this);
and you are done: 您已经完成:
public void addMovie(@NotNull Movie movie) {
if (this.movies == null)
this.movies = new ArrayList<>();
this.movies.add(movie);
movie.setPerson(this);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.