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/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 + " ";
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 + "' οnclick=shoppingcat('testCaseDiv" + i + "','testCaseSpan" + i + "') title=open><img src='./images/down.png'/></span>";
url = url + " ";
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> </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);
}
...........................................
}
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值