加了 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异常
不管是单表 还是连表 都不行。
值是有获取到,出错信息:
有两个:
第一个 是说我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异常
解决方案 »
- java获取邮件的未读或已读状态
- 在JSP页面中怎么实现翻页功能
- 求解:一道Java的课后作业题
- 关于struts2 我后台需要一个对象,前台怎么编写?
- 字符串截取
- Spring 注入问题
- 【300分】高分求Altova XMLSpy Enterprise Edition 2006 rel.3 sp2 的注册机
- [JAVA] 如何判斷一個(boolean)布爾變量的取值﹖
- 有用过eWebEditor(飞鱼控件)的进高手来帮个忙~~谢谢
- eclipse3.0.1+tomcat5.0.28+lomboz3.0.1运行j2ee问题
- Spring 事件广播监听的问题
- js迭代radio 后台action如何接受radio的值
<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>
明显是需要转换为json的对象中有一个子对象,这个子对象有些属性因为延迟加载的关系,没有初始化,导致框架转JSON报空指针。解决的部分 1,取消延迟延迟加载 。基本不可取,对性能影响大
2,在service层手动调用get方法,。不可去,道理同上。
3,将po,vo分离,vo用于数据逻辑以及转换为JSON数据,PO做数据持久化处理。好好去理解一下延迟加载。
另外好好理解一下po,vo,dto的区别,别把他们混一起。
上面方法不错加上了,但是 又有错误了,关联的数据 为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]"
上面方法不错加上了,但是 又有错误了,关联的数据 为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]"无法初始化
如果不需要用到Product里的carts的话,就用JsonConfig过滤掉;
否则就在查询的时候把carts对象也查出来。
这是hibernate懒加载机制引起的,在put对象的时候,对象还关联了其他的表,当json对对象进行解析的时候,去访问对象的属性,这时候hibernate的Session已经关闭了。
我的做法是 建立一个对象的精简对象,取出不要的属性,就可以解决了。