`

iphone后台-Hibernate_Json过滤引起循环的字段

    博客分类:
  • SSH
阅读更多
一定要说明:这不是我写的,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/childrenAjax.html",
    dataType: "json",
    data: url,
    error: function(){
     alert("<fmt:message key='errors.msg'/>");
    },
    success: function(result){
      var org1=eval("("+ result + ")"); 
      for(var i=0;i<org1.length;i++){ 
       menuTree.add(org1[i].id,ID,org1[i].id + " : " + org1[i].name);  
      }  
    }
  });
3.Map对象
  $.ajax({
   type: "POST",
   url: "ajax/associationAjax.html",
            dataType: "json",
   data: "associationObjectIds=" + idString () ,
   success: function(result){
    var json=eval("("+ result + ")");
    var dataItems = json.dataItems;
    var testCases = json.testCases;
    var sysReqs = json.sysReqs;
    var hlrReqs = json.hlrReqs;
    var llrReqs = json.llrReqs;
    var funcInfos = json.funcInfos;
    .....................................................
    if (testCases.length != 0){
             $('#testCaseDiv').empty();
             var url = "";
             for(var i =0;i<testCases.length;i++){
               url = url + "<div id='testCaseDiv" + i + "' style='height:30px;overflow:hidden;'>";
               url = url + "<table width=100% border=1 cellspacing=0 cellpadding=1 bordercolor=ffffff bordercolordark=#ffffff bordercolorlight=B9B9B9>";
               url = url + "<tr align=center>";
               url = url + "<td width=20% height=28 bgcolor=#EFEFEF><fmt:message key='summary.html.testCase.Id'/></td>";
               url = url + "<td width=30% height=28 align=left>";
               url = url + testCases[i].id + "&nbsp;";
               url = url + "</td>";
               url = url + "<td width=20% height=28  bgcolor=#EFEFEF><fmt:message key='summary.html.testCase.creator'/></td>";
               url = url + "<td  width=30% height=28  align=left >";
               url = url + "<span style='float:left' id='testCaseSpan" + i + ".creator'>" + testCases[i].creatorName + "</span>"; 
               url = url + "<span style='float:right' id='testCaseSpan" + i + "' onclick=shoppingcat('testCaseDiv" + i + "','testCaseSpan" + i + "')  title=open><img src='./images/down.png'/></span>";
               url = url + "&nbsp;";
               url = url + "</td>";
               url = url + "</tr>";
               url = url + "<tr align=center>";
               url = url + "<td width=20% height=28 bgcolor=#EFEFEF><fmt:message key='summary.html.testCase.name'/></td>";
               url = url + "<td colspan=3 height=28  align=left><span  id='testCaseSpan" + i + ".name'>" + testCases[i].name + "</span>&nbsp;</td>";
               url = url + "</tr>";
               url = url + "</tr>";
               url = url + "<tr align=center>";
               url = url + "<td width=20% height=60 bgcolor=#EFEFEF><fmt:message key='summary.html.testCase.description'/></td>";
               url = url + "<td width=80% height=60 colspan=3 align=left ><span  id='testCaseSpan" + i + ".description'>" + testCases[i].description + "</span>";
               url = url + "</td>";
               url = url + "</tr>";
               url = url + "</table>";
               url = url + "</div>"; 
             }
             $('#testCaseDiv').append(url);
       }
    ...........................................
   }
  });
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics