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

 因为项目中使用了Extjs技术,JAR包为:json-lib.jar, 在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题:

net.sf.json.JSONException: There is a cycle in the hierarchy!
 at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
 at net.sf.json.JSONObject._fromBean(JSONObject.java:857)
 at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
 at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
 at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)
 at net.sf.json.JSONObject.setValue(JSONObject.java:1507)
 at net.sf.json.JSONObject._fromBean(JSONObject.java:940)
 at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
 at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
 at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)
 at net.sf.json.JSONObject.setValue(JSONObject.java:1507)
 at net.sf.json.JSONObject._fromBean(JSONObject.java:940)
 at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
 at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
 at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)
 at net.sf.json.JSONObject.setValue(JSONObject.java:1507)
 at net.sf.json.JSONObject._fromBean(JSONObject.java:940)
 at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
 at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
 at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)
 at net.sf.json.JSONObject.setValue(JSONObject.java:1507)
 at net.sf.json.JSONObject._fromBean(JSONObject.java:940)
 at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
 at net.yanhl.iouser.action.IOUserAction.loadUser(IOUserAction.java:142)

 

因为Hibernate中设置了自身关联:

 

 

//设置自身关联的组对象

public class User implements Serializable {

 private static final long serialVersionUID = 6202253180943473205L;
 @ManyToMany(targetEntity = Role.class, fetch = FetchType.EAGER)
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
 private Set<Role> roles;
 
 @Transient
 private Map<String, List<Resource>> roleResources;

 

 

起初想通过hibernate来解决问题,就是想过滤掉自身关联后来查资料发现不可能实现,最后找到通过JSON-LIB来过滤关联的集合属性,代码如下:

JsonConfig config = new JsonConfig();
   config.setJsonPropertyFilter(new PropertyFilter(){
    public boolean apply(Object source, String name, Object value) {
     if(name.equals("roles") || name.equals("roleResources")) {
      return true;
     } else {
      return false;
     }
    }
   
   });
   List<User> list = (List<User>)userManagerImpl.findPaginatedList(user, perpage, current);
   JSONArray arr = JSONArray.fromObject(list, config);

 

当JSON-LIB解析JAVABEAN时过滤掉roles、roleResources这两个属性,重新启动服务,pass

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

無名VF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值