加了 lazy="false" 也不行。
不管是单表 还是连表 都不行。
值是有获取到,出错信息:
有两个:
第一个 是说我id有问题无语
第二个:
HTTP Status 500 - org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.shop.bean.Product.carts, no session or session was closedtype Exception reportmessage org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.shop.bean.Product.carts, no session or session was closeddescription The server encountered an internal error that prevented it from fulfilling this request.exceptionorg.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.shop.bean.Product.carts, no session or session was closed
org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:246)
org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178)
org.apache.struts2.json.JSONWriter.process(JSONWriter.java:168)
org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134)
org.apache.struts2.json.JSONWriter.map(JSONWriter.java:437)
org.apache.struts2.json.JSONWriter.process(JSONWriter.java:154)
org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134)
org.apache.struts2.json.JSONWriter.write(JSONWriter.java:102)
org.apache.struts2.json.JSONUtil.serialize(JSONUtil.java:116)
org.apache.struts2.json.JSONResult.createJSONString(JSONResult.java:197)
org.apache.struts2.json.JSONResult.execute(JSONResult.java:171)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:367)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:271)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaJSON对象Strutssession异常

解决方案 »

  1.   

    web.xml里加上<!-- OpenSessionInViewFilter过滤器 -->
    <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
      

  2.   

    没什么说的,验证加载的问题。
    明显是需要转换为json的对象中有一个子对象,这个子对象有些属性因为延迟加载的关系,没有初始化,导致框架转JSON报空指针。解决的部分 1,取消延迟延迟加载 。基本不可取,对性能影响大
             2,在service层手动调用get方法,。不可去,道理同上。
             3,将po,vo分离,vo用于数据逻辑以及转换为JSON数据,PO做数据持久化处理。好好去理解一下延迟加载。
    另外好好理解一下po,vo,dto的区别,别把他们混一起。
      

  3.   


    上面方法不错加上了,但是 又有错误了,关联的数据 为null 报错了。org.springframework.dao.InvalidDataAccessResourceUsageException: could not initialize a collection: [com.shop.bean.Product.orders#1]; nested exception is org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.shop.bean.Product.orders#1]"
      

  4.   


    上面方法不错加上了,但是 又有错误了,关联的数据 为null 报错了。org.springframework.dao.InvalidDataAccessResourceUsageException: could not initialize a collection: [com.shop.bean.Product.orders#1]; nested exception is org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.shop.bean.Product.orders#1]"无法初始化 
      

  5.   

    3L正解;
    如果不需要用到Product里的carts的话,就用JsonConfig过滤掉;
    否则就在查询的时候把carts对象也查出来。
      

  6.   

    有解决办法了。
    这是hibernate懒加载机制引起的,在put对象的时候,对象还关联了其他的表,当json对对象进行解析的时候,去访问对象的属性,这时候hibernate的Session已经关闭了。
    我的做法是 建立一个对象的精简对象,取出不要的属性,就可以解决了。