jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy

  • Post author:
  • Post category:其他


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

这个问题一看就是hibernate的关联问题,在处理json的时候,没有能够转换正确,话不多说,先上代码

List<TrLinePicture> respicList=this.commonMng.findByProperties(TrLinePicture.class, new String[]{“trLineInfo.id”}, new Object[]{id},new String[]{“orderNum”},new String[]{“asc”});

JsonConfig config=new JsonConfig();

config.setJsonPropertyFilter(new PropertyFilter() {


@Override

public boolean apply(Object arg0, String arg1, Object arg2) {


if(arg1.equals(“trLineInfo”)){//过滤掉trLineInfo属性

return true;

}else{


return false;

}

}

});

JSONArray jsonArray=JSONArray.fromObject(respicList,config);

这边的需求是线路主表、以及线路图片子表,具体的子表配置如下

<?xml version=”1.0″?>

<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>

<hibernate-mapping package=”com.ldcms.ld.entity.travel”>

<class name=”TrLinePicture” table=”tr_line_pictures” dynamic-update=”true”>

<meta attribute=”sync-DAO”>false</meta>

<cache usage=”read-write”/>

<id name=”id” type=”java.lang.Integer” column=”pk_pic_id”><generator class=”sequence”><param name=”sequence”>TR_LINE_PICTURES_SEQ</param></generator></id>

<property name=”picPath” column=”pic_path” type=”string” not-null=”false” length=”30″/>

<property name=”orderNum” column=”order_num” type=”integer” not-null=”false” length=”10″/>

<property name=”isTitlePic” column=”is_title_pic” type=”string” not-null=”true” length=”1″/>

<many-to-one name=”trLineInfo” column=”fk_line_info_id” class=”TrLineInfo” not-null=”true”></many-to-one>

</class>

</hibernate-mapping>

同时在主表(线路表)上也配置了线路图片的关联关系

此时是双向关联,当你在JSONArray.fromObject  子表 的时候,如果不去除主表的配置属性,此时会出现死循环

所以需要上述代码进行处理,所以结果就ok啦!!



版权声明:本文为luxingyuan_jmi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。