There is a cycle in the hierarchy!" json
There is a cycle in the hierarchy!" json
I am getting one JSON exception called "net.sf.json.JSONException: There is a cycle in the hierarchy!"
here is code
Query q = em.createQuery("SELECT e from employee e ");
List<Employee> employeeList = q.getResultList();
JSONObject response = new JSONObject();
response.put("empList", employeeList);
here is Employee Entity. it has one to many relation
public class Employee {
@Id
private String userId;
@JoinColumn(name = "T_MENTORS_userId", referencedColumnName = "userId")
private Collection<Experience> experience;
}
3 Answers
3
You may be running into a circular reference. Does Experience
refer to Employee
and Employee
refers to Experience
?
Experience
Employee
Employee
Experience
If so, there is a couple of solutions:
Employee
Experience
Employee
Experience
Employee
See Martin Fowler for an overview of DTO:
http://martinfowler.com/eaaCatalog/dataTransferObject.html
In this situation, according to your business logic, you can delete one of reference, for example :
Employee{
private Collection<Experience> experiences;
}
and in Experience
Experience
{
// no reference to Employee
}
If you don't want to remove the reference from the entity, there is a workaround.
Use,
JSONObject response = new JSONObject();
response.getJsonConfig().
setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT)
response.put("empList", employeeList);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Do you understand the problem? What do you want to do about it?
– SLaks
Jun 30 '13 at 16:54