I have the following classes :
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. */
}
Now, for some reason I am getting an infinite loop when I try to work with these links... I thought lazy = true prevented this, does anybody know what I am doing wrong?
There is an issue on Spring Data Mongo which discusses the option to make DbRefs lazy by default in order to avoid such stack overflows. It also refers to two other issues that trigger the same infinite recursion:
a StackOverflow exception can only happen when DBRef s, whether lazy or not, are placed in the constructor.
Not sure if the "only" is a typo. And
if someone overrides Object's methods ("equals", "hashCode" or "toString") in its @Document entity that is likely to trigger the resolution of the DBRef
So a possible reason for your problem is, that something else is triggering the recursive resolution.
You might find the culprit by inspecting the top of the stack trace.
UPDATE
Based on your comment, this does not seem to be Spring Data related, but a problem with Jackson.
There are actually answers available on SO how to solve this problem. This one sounds promising to me https://stackoverflow.com/a/4126846/66686