日期:2014-05-16  浏览次数:20303 次

iphone后台-Hibernate_Json过滤引起循环的字段
一定要说明:这不是我写的,copy的,地址:http://java20100406.iteye.com/blog/946580
=======================>自己添加<============================
Hibernate many-to-one双向关联中,查询many方时会将one方数据顺带着查询,同时one中会有List<Many>,然后又会去查Many中的数据...周而复始,结果=>完了,json解析失败。
解决方法,通过JsonConfig设置过滤字段(setExcludes)
举例如:

**********===model===************
public class One {
	private int id;
	List<Many> manyList;
         ...
}

public class Many {
	private int id;
	private One one;
         ...
}

**********   daoImpl   ************
String HQLStr = "from Many";


**********   action   ************
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(new String[]{"manyList"});//关键在这,过滤掉这个
JSONArray jsonArr = new JSONArray();
jsonArr.add(tempList, jsonConfig);//tempList是通过Hibernate查询的结果集


=======================>自己添加<============================


正文如下:
通过配置JsonConfig过滤有可能引起循环的字段,如果不这样,根据hibernate的原则会不断的级联查询.
*************************************Action******************************************************
1.对象。
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes( new String[]{ "creator", "module", "testPoints","testProcesses","highLevelRequirements","lowLevelRequirements"} );
String result = JSONObject.fromObject( testCase对象 ,jsonConfig).toString();
2.List数组
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes( new String[]{ "creator", "module", "testPoints","testProcesses","highLevelRequirements","lowLevelRequirements"} );
JSONArray lineitemArray = JSONArray.fromObject( testCases集合List ,jsonConfig);
String result = JSONArray.fromObject(lineitemArray).toString();
3.Map对象
Map map = new HashMap();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes( new String[]{ "creator", "module", "testPoints","testProcesses","highLevelRequirements","lowLevelRequirements"} );
JSONArray lineitemArray = JSONArray.fromObject( testCases集合List ,jsonConfig);
map.put("testCases", lineitemArray);
String result = JSONObject.fromObject( map对象 ).toString();
************************************struts.xml*******************************************************
<package name="ajax" extends="json-default" namespace="/ajax">
..........
        <action name="childrenAjax" class="**********.AssociationBaseAction" method="getChildren">
            <result name="success" type="json">
                <param name="root">result</param>
            </result>
        </action>
..........
</package>
***************************************javascript****************************************************
1.对象。
  $.ajax({
   type: "POST",
   url: "ajax/oneAjax.html",
            dataType: "json",
   data: "targetId=" + val ,
   success: function(result){
    var json=eval("("+ result + ")");
    $('#selectedResult').append(  "<div id=" + json.id +"><input type='checkbox' name='selected' value='" + json.id + "'>"+ json.id + " : " + json.name +"</div>"  );
   }
  });
2.List数组
  $.ajax({
    type: "POST",
    url: "ajax/chi