当前位置:首页 > 开发 > 编程语言 > Json > 正文

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

发表于: 2014-06-16   作者:adminjun   来源:转载   浏览:
摘要: 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

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);

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

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
为什么需要延迟加载? 因为会提高效率嘛,存在即合理,哈哈.一般设置延迟加载就是在对象关系映射上(在
问题场景 好久不写BLOG了,最近搬家,重新办了网络,发现一样的路由器,到了这边ping值基本上没有个
傅型号值问题: 首先须要说明的是:Action在请求到达ActionProxy时已经创建出来了,而且对应的创建
先上效果图,已在Firefox3.5、IE7、Chrome3中测试正常 完整应用和代码请转到 http://www.iteye.com/
先上效果图,已在Firefox3.5、IE7、Chrome3中测试正常 完整应用和代码请转到 http://www.iteye.com/
1.背景介绍   最近项目要绘制股票走势图,并绘制能够跟随手指滑动的指示线(Indicator)来精确查看
在mac系统中,想用office软件,可用mac office2011版本。但不是所有windows软件都有对应的mac软件替
1.背景介绍   最近项目要绘制股票走势图,并绘制能够跟随手指滑动的指示线(Indicator)来精确查看
感谢作者,支持原创。 近日在做一个分布式转码服务器,解码器是采用开源的ffmpeg,在开发的过程中遇
近日在做一个分布式转码服务器,解码器是采用开源的ffmpeg,在开发的过程中遇到一个问题:编码延迟
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号