天天看點

json-lib出現There is a cycle in the hierarchy解決辦法

因為項目中使用了AJAX技術,JAR包為:json-lib.jar, 在開發過程中遇到了一個JSON-LIB和Hibernate有關的問題:

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:857)

at net.sf.json.JSONObject.fromObject(JSONObject.java:192)

at net.sf.json.JSONObject._processValue(JSONObject.java:2774)

at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)

at net.sf.json.JSONObject.setValue(JSONObject.java:1507)

at net.sf.json.JSONObject._fromBean(JSONObject.java:940)

at net.sf.json.JSONObject.fromObject(JSONObject.java:192)

at net.sf.json.JSONObject._processValue(JSONObject.java:2774)

at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)

at net.sf.json.JSONObject.setValue(JSONObject.java:1507)

at net.sf.json.JSONObject._fromBean(JSONObject.java:940)

at net.sf.json.JSONObject.fromObject(JSONObject.java:192)

at net.sf.json.JSONObject._processValue(JSONObject.java:2774)

at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)

at net.sf.json.JSONObject.setValue(JSONObject.java:1507)

at net.sf.json.JSONObject._fromBean(JSONObject.java:940)

at net.sf.json.JSONObject.fromObject(JSONObject.java:192)

at net.sf.json.JSONObject._processValue(JSONObject.java:2774)

at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)

at net.sf.json.JSONObject.setValue(JSONObject.java:1507)

at net.sf.json.JSONObject._fromBean(JSONObject.java:940)

at net.sf.json.JSONObject.fromObject(JSONObject.java:192)

at net.yanhl.iouser.action.IOUserAction.loadUser(IOUserAction.java:142)

因為Hibernate中設定了自身關聯:

Iouser.hbm.xml:

<many-to-one name="group" class="net.yanhl.iouser.pojo.GroupRelation" lazy="false" cascade="none">

<column name="group_id" />

</many-to-one>

//設定自身關聯的組對象

public class GroupRelation implements Serializable {

private static final long serialVersionUID = 6202253180943473205L;

private Integer id;// 主鍵ID

private Integer creatorId;// 建立人

private Date createDate;// 建立日期

private String groupName;// 組名稱

private GroupRelation parentGroup;

private Set<GroupRelation> childGroups = new HashSet<GroupRelation>();

<many-to-one name="parentGroup" column="parent_id" lazy="false"

class="net.yanhl.iouser.pojo.GroupRelation">

</many-to-one>

<set name="childGroups" cascade="save-update" inverse="true">

<key column="parent_id"></key>

<one-to-many class="net.yanhl.iouser.pojo.GroupRelation" />

</set>

起初想通過hibernate來解決問題,就是想過濾掉自身關聯後來查資料發現不可能實作,最後找到通過JSON-LIB來過濾關聯的集合屬性,代碼如下:

JsonConfig config = new JsonConfig();

config.setJsonPropertyFilter(new PropertyFilter(){

public boolean apply(Object source, String name, Object value) {

if(name.equals("parentGroup") || name.equals("childGroups")) {

return true;

} else {

return false;

}

}

});

Iouser user = (Iouser) getBaseManager().get(Iouser.class, iouserId);

JSONObject jsonObject = JSONObject.fromObject(user, config);

當JSON-LIB解析JAVABEAN時過濾掉parentGroup、childGroups這兩個屬性,重新啟動服務,pass