net.sf.json.JSONException: There is a cycle in the hierarchy!---数据间存在级联关系,而互相嵌套引用,在hibernate中极容易嵌套而抛
来源:互联网 发布:室内三维设计软件 编辑:程序博客网 时间:2024/05/21 01:46
问题:
net.sf.json.JSONException: There is a cycle in the hierarchy!
at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
***************************************************************************************************************************************************
在查问一些资料后,总结出解决该问题的两种办法,现在与大家分享一下。
其一:根据原理来解决,如果需要解析的数据间存在级联关系,而互相嵌套引用,在hibernate中极容易嵌套而抛出net.sf.json.JSONException: There is a cycle in the hierarchy异常。
举个例子:现在有实验(Lib)和类别(Libtype)两张表,每个实验都对应着一个类别,那么,在类别的POJO中,就会如下代码:
private Integer ltid; //类别ID
private String ltype; //类别名称
private Set libs = new HashSet(0); //对应的实验集
当我们写如下代码时,会报错:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
LibtypeDAO libtypeDAO = new LibtypeDAO();
List<Libtype> list = libtypeDAO.findAll();
JSONArray jsonArray = JSONArray.fromObject(list);
return null;
}
原因很简单,在Libtype中,有一个与List无关的属性值,即libs,我们只需要ltid和ltype,所以报错。
根据我在网上查找的资料,解决办法有如下3种:
1.设置JSON-LIB让其过滤掉引起循环的字段:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
LibtypeDAO libtypeDAO = new LibtypeDAO();
List<Libtype> list = libtypeDAO.findAll();
JsonConfig jsonConfig = new JsonConfig(); //建立配置文件
jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略
jsonConfig.setExcludes(new String[]{"libs"}); //此处是亮点,只要将所需忽略字段加到数组中即可,在上述案例中,所要忽略的是“libs”,那么将其添到数组中即可,在实际测试中,我发现在所返回数组中,存在大量无用属性,如“multipartRequestHandler”,“servletWrapper”,那么也可以将这两个加到忽略数组中.
JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig); //加载配置文件
return null;
}
2.设置JSON-LIB的setCycleDetectionStrategy属性让其自己处理循环,省事但是数据过于复杂的话会引起数据溢出或者效率低下。
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
LibtypeDAO libtypeDAO = new LibtypeDAO();
List<Libtype> list = libtypeDAO.findAll();
JsonConfig jsonConfig = new JsonConfig(); //建立配置文件
jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); //此处是亮点,不过经过测试,第2种方法有些悲剧,虽然可以使用,但其结果貌似循环数次,至于为啥,还请高人指点。
JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig); //加载配置文件
return null;
}
3.最为原始的办法,自己写个JavaBean,用forEach循环,添加到List中,这个方法我看网上有人成功,我没试,但大概过程可以写出来,其结果正确性有待检验。
JavaBean:
public LibtypeForm{
int ltid;
string ltname;
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
LibtypeDAO libtypeDAO = new LibtypeDAO();
List<Libtype> list = libtypeDAO.findAll();
List<LibtypeForm> formList = new ArrayList();
for(Libtype libtype : list){
LibtypeForm form = new LibtypeForm();
form.setLtid(libtype .getLtid);
form.setLtname(libtype.getLtname);
formList.add(form);
}
JSONArray jsonArray = JSONArray.fromObject(formList);
return null;
}
如果有更好的方法,还请高人指点。
- net.sf.json.JSONException: There is a cycle in the hierarchy!---数据间存在级联关系,而互相嵌套引用,在hibernate中极容易嵌套而抛
- JSON处理Hibernate实体类net.sf.json.JSONException: There is a cycle in the hierarchy异常
- jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy
- jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy
- json中net.sf.json.JSONException: There is a cycle in the hierarchy!的解决
- Json解析时出现net.sf.json.JSONException: There is a cycle in the hierarchy!42
- jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy
- jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy!
- Hibernate 延迟加载导致的问题 net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException: There is a cycle in the hierarchy异常,解决方法
- net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException: There is a cycle in the hierarchy异常的解决方法
- net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException: There is a cycle in the hierarchy
- net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
- net.sf.json.JSONException: There is a cycle in the hierarchy
- Repeater 时间格式化
- Makefile内置变量,递归式变量,直接展开式变量,条件赋值,追加赋值
- 经营操作系统的一些思路
- WINCE的pb过期解决办法
- 友盟社交分享中的那些坑
- net.sf.json.JSONException: There is a cycle in the hierarchy!---数据间存在级联关系,而互相嵌套引用,在hibernate中极容易嵌套而抛
- 手机第三方支付产品安全性
- Android平台 AES加密/解密
- 找工作总结
- TCP服务端vb.net支持多客户端连接
- 【.Net-码农】在WPF使用FolderBrowserDialog和OpenFileDialog
- ASP.NET多个控件共用一个监听器出错问题解决
- 使用Sqlserver事务发布实现数据同步
- 函数指针作为参数&回调函数