@DBRef 的无限递归问题(lazy = true)
Infinite recursion issue with @DBRef (lazy = true)
我有以下 类 :
public class Person
{
@Id
ObjectId Id;
String name;
@DBRef(lazy = true)
List<Entity> entities;
/* Getters and setters omitted for brevity. */
}
public class Entity
{
@Id
ObjectId Id;
String entityName;
@DBRef(lazy = true)
List<Person> people;
/* Getters and setters omitted for brevity. */
}
现在,出于某种原因,当我尝试使用这些链接时出现无限循环...我认为 lazy = true 阻止了这种情况,有人知道我做错了什么吗?
Spring Data Mongo 上有一个问题讨论了默认情况下使 DbRefs 懒惰以避免此类堆栈溢出的选项。它还提到了触发相同无限递归的另外两个问题:
a Whosebug exception can only happen when DBRef s, whether lazy or not, are placed in the constructor.
不确定 "only" 是否有错字。并且
if someone overrides Object's methods ("equals", "hashCode" or "toString") in its @Document entity that is likely to trigger the resolution of the DBRef
所以你的问题的一个可能原因是,其他东西触发了递归解析。
您可能会通过检查堆栈跟踪的顶部找到罪魁祸首。
更新
根据您的评论,这似乎与 Spring 数据无关,而是 Jackson 的问题。
关于如何解决这个问题实际上有可用的答案。这个听起来很有希望
我有以下 类 :
public class Person
{
@Id
ObjectId Id;
String name;
@DBRef(lazy = true)
List<Entity> entities;
/* Getters and setters omitted for brevity. */
}
public class Entity
{
@Id
ObjectId Id;
String entityName;
@DBRef(lazy = true)
List<Person> people;
/* Getters and setters omitted for brevity. */
}
现在,出于某种原因,当我尝试使用这些链接时出现无限循环...我认为 lazy = true 阻止了这种情况,有人知道我做错了什么吗?
Spring Data Mongo 上有一个问题讨论了默认情况下使 DbRefs 懒惰以避免此类堆栈溢出的选项。它还提到了触发相同无限递归的另外两个问题:
a Whosebug exception can only happen when DBRef s, whether lazy or not, are placed in the constructor.
不确定 "only" 是否有错字。并且
if someone overrides Object's methods ("equals", "hashCode" or "toString") in its @Document entity that is likely to trigger the resolution of the DBRef
所以你的问题的一个可能原因是,其他东西触发了递归解析。
您可能会通过检查堆栈跟踪的顶部找到罪魁祸首。
更新
根据您的评论,这似乎与 Spring 数据无关,而是 Jackson 的问题。
关于如何解决这个问题实际上有可用的答案。这个听起来很有希望