Total Pageviews

2011/09/08

net.sf.json.JSONException: There is a cycle in the hierarchy!


Problem
I apply JPA(Java Persistence API) to do search via primary key, then it return an object to me. And I would like to covert this object to JSONObject, it show this error message.

1
2
3
4
5
6
7
net.sf.json.JSONException: There is a cycle in the hierarchy! 
at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97) 
at net.sf.json.JSONObject._fromBean(JSONObject.java:674)
at net.sf.json.JSONObject.fromObject(JSONObject.java:181) 
at net.sf.json.JSONArray._processValue(JSONArray.java:2381) 
at net.sf.json.JSONArray.processValue(JSONArray.java:2412) Truncated. 
see log file for complete stacktrace


The code snippet as shown below


Root Cause
Because the entity you return has many-to-one relationship, so you need to filter them out.

Solution



Reference: http://json-lib.sourceforge.net/snippets.html
Post a Comment