延迟加载问题,用json过滤解决

2014-06-16 15:58:49,761 [http-8080-3] ERROR [org.hibernate.LazyInitializationException] - failed to lazily initialize a collection of role: com.wiseweb.pom.entity.Role.docInfos, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.wiseweb.pom.entity.Role.docInfos, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
    at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
    at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:158)
    at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134)
    at org.apache.struts2.json.JSONWriter.add(JSONWriter.java:390)
    at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:231)
    at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178)
    at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:168)
    at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134)
    at org.apache.struts2.json.JSONWriter.add(JSONWriter.java:390)
    at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:231)
    at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178)
    at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:168)
    at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134)
    at org.apache.struts2.json.JSONWriter.write(JSONWriter.java:102)
    at org.apache.struts2.json.JSONUtil.serialize(JSONUtil.java:116)
    at org.apache.struts2.json.JSONResult.createJSONString(JSONResult.java:197)
    at org.apache.struts2.json.JSONResult.execute(JSONResult.java:171)

··················································

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.wiseweb.pom.entity.Role.docInfos, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
    at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
    at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:158)
    at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134)
    at org.apache.struts2.json.JSONWriter.add(JSONWriter.java:390)
    at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:231)
    ... 110 more

今天做项目遇到这个问题,第一反应是延迟加载问题,应该是没有设置lazy-init,过去看看确实没有设置,再到Action中查看报错方法,发现方法根本就没有使用这个,研究了好长时间发现我的实体是存在session中的,类似于一个json串,所以在使用的时候就会报这个错,最终的解决方法是做json过滤,把报错的这个字段给过滤掉就不会有这个问题了。过滤的代码如下:

loginRole = ((LoginUser) ((SecurityContext) ServletActionContext
                .getRequest().getSession().getAttribute(
                        "SPRING_SECURITY_CONTEXT")).getAuthentication()
                .getPrincipal()).getRole();
       
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setRootClass(Role.class);
        jsonConfig.setJsonPropertyFilter(new net.sf.json.util.PropertyFilter() {
           
            @Override
            public boolean apply(Object arg0, String arg1, Object arg2) {
                if(arg1.equals("docInfos")){
                    return true;
                }else {
                    return false;
                }
            }
        });
        net.sf.json.JSON jsonObject = JSONSerializer.toJSON(loginRole, jsonConfig);
        loginRole = (Role)JSONSerializer.toJava(jsonObject, jsonConfig);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值