因為項目中使用了AJAX技術(shù),JAR包為:json-lib.jar, 在開發(fā)過程中遇到了一個JSON-LIB和Hibernate有關(guān)的問題:
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中設(shè)置了自身關(guān)聯(lián):
Iouser.hbm.xml:
//設(shè)置自身關(guān)聯(lián)的組對象
public class GroupRelation implements Serializable {
private static final long serialVersionUID = 6202253180943473205L;
private Integer id;// 主鍵ID
private Integer creatorId;// 創(chuàng)建人
private Date createDate;// 創(chuàng)建日期
private String groupName;// 組名稱
private GroupRelation parentGroup;
private Set childGroups = new HashSet();
class="net.yanhl.iouser.pojo.GroupRelation">
起初想通過hibernate來解決問題,就是想過濾掉自身關(guān)聯(lián)后來查資料發(fā)現(xiàn)不可能實現(xiàn),考試,大提示通過JSON-LIB來過濾關(guān)聯(lián)的集合屬性,代碼如下:
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);
當(dāng)JSON-LIB解析JAVABEAN時過濾掉parentGroup、childGroups這兩個屬性,重新啟動服務(wù),pass
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中設(shè)置了自身關(guān)聯(lián):
Iouser.hbm.xml:
//設(shè)置自身關(guān)聯(lián)的組對象
public class GroupRelation implements Serializable {
private static final long serialVersionUID = 6202253180943473205L;
private Integer id;// 主鍵ID
private Integer creatorId;// 創(chuàng)建人
private Date createDate;// 創(chuàng)建日期
private String groupName;// 組名稱
private GroupRelation parentGroup;
private Set
起初想通過hibernate來解決問題,就是想過濾掉自身關(guān)聯(lián)后來查資料發(fā)現(xiàn)不可能實現(xiàn),考試,大提示通過JSON-LIB來過濾關(guān)聯(lián)的集合屬性,代碼如下:
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);
當(dāng)JSON-LIB解析JAVABEAN時過濾掉parentGroup、childGroups這兩個屬性,重新啟動服務(wù),pass