neo4j - Neo4j-Spring-Data 深度功能不适用于丰富的关系
问题描述
我正在使用来自 Neo4j“Neo4j-OGM”的 API,但我在尝试使用 findAll(int depth) 方法时遇到了困难。我的问题是,当我拥有丰富的关系时,深度就会丢失。在某些情况下它可以正常工作,例如 findAll(0)、findById(int id, int depth)、findAll(...) 当结果为 1 时,或者当我有一个简单的关系时。我将分享我的数据结构和 jsons。
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"=depth)发出 get 请求时
[{
{
"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 缓存在事务/会话期间加载的所有实体。在 a 的情况下,findAll
它收集所有已加载的实体并将它们分配给它们的关系。这意味着,如果它在Wlisses 的朋友的第二层找到Sui (正如您定义的深度),如果它是匹配关系,它将在Tony的结果中将其附加到Fernando 。
目前 Neo4j-OGM 中没有机制可以避免这种情况。
推荐阅读
- php - 在 Google App Engine 柔性环境 PHP (Laravel) 上启用 CORS
- ruby - Ruby 编译时出现 ocra 错误 - 找不到 libssp-0.dll
- javascript - Pressmove in javascript ,有没有办法检测鼠标或手指触摸移动的元素?
- swift - 应用内购买沙盒环境不工作
- vue.js - vuetify中如何支持大屏自定义tile?
- ngxs - 带有注入的选择器
- java - 最长路径算法 排序路径查找
- python - 为什么Python 3.6默认不安装wheel包?
- html - ul 列表中 li 元素之间的 CSS 间距
- python - 删除和释放单个 pandas 数据帧的内存