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

Keywords: Java JSON Hibernate

Abnormal problem

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:657)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
    at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    at net.sf.json.JSONArray._processValue(JSONArray.java:2513)
    at net.sf.json.JSONArray.processValue(JSONArray.java:2538)
    at net.sf.json.JSONArray.addValue(JSONArray.java:2525)
    at net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)
    at net.sf.json.JSONArray.fromObject(JSONArray.java:123)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:240)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
    at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    at net.sf.json.JSONArray._processValue(JSONArray.java:2513)
    at net.sf.json.JSONArray.processValue(JSONArray.java:2538)
    at net.sf.json.JSONArray.addValue(JSONArray.java:2525)
    at net.sf.json.JSONArray.element(JSONArray.java:1724)
    at net.sf.json.JSONArray.add(JSONArray.java:1249)
    at net.sf.json.JSONArray.add(JSONArray.java:1245)

Cause analysis

Because the internal JSONObject will disassemble the object you passed in indefinitely until there is no disassembly, when parsing beans, there will be a dead cycle call, that is, there will be mutual calls between multiple beans. If the objects you pass in have foreign key relationships or mutual references, the inner loop will die and this exception solution will be thrown. For example, when using Hibernate, objects in a query have multiple table dependency associations.

Solution

Filter out the attributes in the bean that cause the dead cycle call in the result data:

List<DataObject> list= this.baseService.find(xxx); // Result data list DataObject: data object
        
// Custom JsonConfig is used to filter recursive data generated by Hibernate configuration files    
JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{"a","b"}); // Specify which fields and objects to filter
JSONArray result = JSONArray.fromObject(list, config);

Posted by kecebong_soft on Tue, 15 Oct 2019 11:41:39 -0700