Reputation: 3494
I have bi-directional relationship like this...
Person.java
public class Person{
@JsonIgnore
@OneToMany(targetEntity=PersonOrganization.class, cascade=CascadeType.ALL,
fetch=FetchType.EAGER, mappedBy="person")
private Set<PeopleOrg> organization;
.....
}
PersonOrganization.java
public class PersonOrganization{
@JsonIgnore
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PERSONID", nullable=false)
private Person person;
}
Even with @JsonIgnore
annotation I am getting infinite recursion error when trying to retrieve Person records. I have tried new annotations in 1.6 version. @JsonBackReference
and @JsonManagedReference
. Even then I am getting infinite recursion..
With @JsonBackReference("person-organization")
on Person
and @JsonManagedReference("person-organization")
on PersonOrganization
org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.entity.Person["organization"]->org.hibernate.collection.PersistentSet[0]->com.entity.PersonOrganization["person"]->com.entity.Person["organization"]->org.hibernate.collection.PersistentSet[0]...
Even If I interchange the annotations, I am still getting this exception.. Please let me know if there is something wrong with the mappings or the way I am using JSON annotations. Thanks
Upvotes: 24
Views: 35625
Reputation: 11
In my case, I have imported wrong @JsonIgnore annotation like net.minidev.json.annotate.JsonIgnore instead of com.fasterxml.jackson.annotation.JsonIgnore. Changing this import fixed infinite recursion issue for me.
Upvotes: 0
Reputation: 569
for me, i have tried @JsonIgnore, @JsonManagedReference/@JsonBackReference but nothing worked, till i read this Exception thrown ["hibernateLazyInitializer"] solution 1 and this Exception thrown ["hibernateLazyInitializer"] solution 2
solution 1 is to change from fetch.LAZY to fetch.EAGER, and solution 2 is using @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
, and of course use @JsonIgnore in both solutions
Upvotes: 0
Reputation: 726
If A has B & B has A.
This is one to one relationship, but forming a circular relation.
In any of the class, use JustIgnore annotation.
class A
{
B b;
}
class B
{
@JsonIgnore
A a;
}
This applies for other relationships also like one to many.
Upvotes: 1
Reputation: 49
This exception is because, your constructor field are not proper, please check your constructors properties once again in your classes, and check mapping give properly or not,
Keep the two Constructors, first is zero construction and second constructor is with fields and both should be contain the super
Upvotes: 0
Reputation: 569
Apparently since Jackson 1.6 you can use @JsonManagedReference and @JsonBackReference to effectively solve the infinite recursion problem.
I won't go into the details but this changing you classes to the below format should solve the problem.
public class Person{
@OneToMany(targetEntity=PersonOrganization.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="person")
@Column(nullable = true)
@JsonManagedReference
private Set<PeopleOrg> organization;
.....
}
public class PersonOrganization{
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name="PERSONID")
@JsonBackReference
private Person person;
}
Basically Jackson converts Set<PeopleOrg> organization
, the forward part of the reference a to json-like format using the marshalling process, it then looks for Person person
, the back part of the reference and does not serialize it.
Credits - Kurt Bourbaki & More info - http://keenformatics.blogspot.co.ke/2013/08/how-to-solve-json-infinite-recursion.html
Upvotes: 3
Reputation: 103
Jackson works on Reflection by calling getters. I too had such a situation where I had a getter of the same Object inside its class. Jackson went into infinite recursion eating up stack by repeatedly calling its own getter. Removed getter, then it got fixed.
My Advice : If you want to use jackson for conversion of an object, never keep getters which references the same object, like in case of singletons.
Upvotes: -2
Reputation: 15116
Sometimes the member field may have inner reference to the same class type of itself, which could cause infinite recursion when toJson
.
E.g.: you have a member field Klass a
, while the class definition of that Klass is as below.
class Klass {
Klass mySibling;
public toString() {
return "something" + mySibling.whateverMethod;
}
}
Solution: refactor the member field, eliminate the inner reference.
Upvotes: 0
Reputation: 1034
This might be little old but you can add @JsonIgnore at class level with all properties it should ignore. e.g
@JsonIgnore("productImaes","...")
public class Product{ ...
}
Upvotes: 0
Reputation: 3396
Since Jackson 1.6 you can use two annotations to solve the infinite recursion problem without ignoring the getters/setters during serialization: @JsonManagedReference and @JsonBackReference.
For more details refer https://stackoverflow.com/a/18288939/286588
Upvotes: 4
Reputation:
I know this question isn't specifically about Spring Data REST, but I ran into this exception in the context of Spring Data REST, and wanted to share what the problem was. I had a bidirectional relationship involving an entity with no repository. Creating the repository made the loop disappear.
Upvotes: 5
Reputation: 101
The following link says you should annotate the method used by JSON tool to traverse the object graph, to instruct the it to ignore the traversal.
http://jackson.codehaus.org/1.0.1/javadoc/org/codehaus/jackson/annotate/JsonIgnore.html
In my case I have two objects related like this Product <-> ProductImage. So JSON parser went into an infinite loop with out @JsonIgnore annotation on the following to get methods
@JsonIgnore
public Product getImageOfProduct() {
return imageOfProduct;
}
in ProductImage and
@JsonIgnore
public Set<ProductImage> getProductImages() {
return productImages;
}
in Product.
With the annotation, things are working fine.
Upvotes: 10
Reputation: 1076
I've run into this before. But after moving @JsonIgnore from private field to getter of the field, infinite recursion is gone. So my wild guess is that @JsonIgnore might no work on private field. However, javadoc or tutorial of Jackson Java JSON-processor do not mention about this, so I cannot be 100% sure. Just for your information.
Upvotes: 40