我的程序的结构是:
School.java
@NodeEntity
@NoArgsConstructor
@Getter
@Setter
public class School extends Entity {
private String name;
@Relationship(type = "UNDER")
private Board board;
private String address;
private String phone;
@Override
public String toString() {
return "School{" +
"name='" + name + '\'' +
", board=" + board +
", address='" + address + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
Board.java
@NodeEntity
@NoArgsConstructor
@Getter
@Setter
public class Board extends Entity {
private String name;
@Relationship(type = "HAS")
private List<Grade> grades;
@Override
public String toString() {
return "Board{" +
"name='" + name + '\'' +
", grades=" + grades +
'}';
}
}
SchoolRepository.java
@Repository
public interface SchoolRepository extends Neo4jRepository<School, Long> {
}
这是一个春季启动项目,我有相关的控制器和服务。
问题是schoolRepository.findAll()
对于grades
中存在的board
返回空值。
但是完整的数据显示在数据库中。
输出,我大摇大摆:
[
{
"id": 5,
"name": "Bishops",
"board": {
"id": 14,
"name": "CBSE",
"grades": null
},
"address": "Nagpur",
"phone": "1234567891"
},
{
"id": 15111,
"name": "Conrads",
"board": {
"id": 15132,
"name": "ICSE",
"grades": null
},
"address": "Agra",
"phone": "789456123"
}
]
我在做什么错?
这是因为findAll
的默认深度为1。因此,由于等级位于学校的深度2,因此School
及其Board
被加载,但Grades
的Board
未加载。 >
如果将深度参数2传递给findAll
,将加载坡度。