我正在使用Neo4j“ Neo4j-OGM”中的API,尝试使用findAll(int depth)方法时遇到麻烦。我的问题是,当我与人之间有丰富的关系时,深度就会迷失。在某些情况下,它可以正常工作,例如findAll(0),findById(int id,int深度),findAll(...)(当结果为1或我有简单关系时)。我将分享我的数据结构和json。
Person.class和Friend.class
@NodeEntity
@Data
public class Person{
@Id
private String id;
private String name;
@Property("born")
private int birthyear;
@JsonIgnoreProperties({"person1"})
@Relationship(type = "FRIEND_OF")
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<Friend> friendOfs;
@JsonIgnoreProperties({"person"})
@Relationship(type = "FRIEND_OF",direction = Relationship.INCOMING)
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<Friend> friendOfMy ;
}
@Data
@RelationshipEntity(value = "FRIEND_OF")
public class Friend {
@Id
@GeneratedValue
private Long id;
private String since;
@StartNode
@JsonIgnoreProperties({"friendOfs"})
private Person person1;
@EndNode
@JsonIgnoreProperties({"friendOfMy"})
private Person person;
}
[当我向http://localhost:8080/rest/persons/all/2-发出获取请求时](“ 2” =深度)
[{
{
"id": "24",
"name": "Tony",
"birthyear": 0,
"friendOfMy": [
{
"id": 0,
"since": "2008-06-06",
"person1": {
"id": "2",
"name": "Lina",
"birthyear": 0,
"friendOfMy": [
{
"id": 460,
"since": "2008-06-06",
"person1": {
"id": "1",
"name": "Fernando",
"birthyear": 0,
"friendOfMy": [
{
"id": 462,
"since": "2008-06-06",
"person1": {
"id": "4",
"name": "Sui",
"birthyear": 0
}
}
]
}
}
]
}
}
]},
{
"id": "3",
"name": "Wlisses",
"birthyear": 0,
"friendOfMy": [
{
"id": 461,
"since": "2000-06-06",
"person1": {
"id": "1",
"name": "Fernando",
"birthyear": 0,
"friendOfMy": [
{
"id": 462,
"since": "2008-06-06",
"person1": {
"id": "4",
"name": "Sui",
"birthyear": 0
}
}
]
}
}
]
},
.
.
.
{
"id": "1",
"name": "Fernando",
"birthyear": 0,
"friendOfs": [
{
"id": 461,
"since": "2000-06-06",
"person": {
"id": "3",
"name": "Wlisses",
"birthyear": 0
}
},
{
"id": 460,
"since": "2008-06-06",
"person": {
"id": "2",
"name": "Lina",
"birthyear": 0,
"friendOfs": [
{
"id": 0,
"since": "2008-06-06",
"person": {
"id": "24",
"name": "Tony",
"birthyear": 0
}
}
]
}
}
],
"friendOfMy": [...]
}]
如您所见,第一个结果带来深度3。我的猜测是由于Java的内存引用而发生的。
我想知道它是否可以解决,或者我做错了什么。
不,您在这里没有做错任何事情,您的假设是正确的。Neo4j-OGM缓存在事务/会话期间加载的所有实体。在findAll
的情况下,它将收集所有已加载的实体,并将它们分配给它们的关系。这意味着,如果它在第二级上找到了[[Sui-如您所定义的Wlisses'朋友的深度-,它将在Tony的结果中将其附加到Fernando这是一个匹配的关系。