我在spring mvc中配置json转换 controller中的对象的时候,如果是 hibernate 懒加载的对象就会出现 session已经关闭的错误,因为事物是在service层,查了一些解决办法如下:
1.在web.xml中加入OpenSessionInViewFilter ,这样的话session其实是未关闭的,前台页面如果调用get方法还是会去请求数据库 不能这样做啊
2.在实体bean的set属性上添加jsonignore ,这样虽然不会报错,但是在任何请查询结果中前台只能得到主对象了 ,这样还是不行 果断不能这样做
放弃lazy load 更不能这样做了
网上提出这个问题 的人很多但是我觉得以上解决办法都不好或者是不能满足需求
求大牛帮忙解决。。。。。
伊谢尔伦2017-04-17 17:59:54
I don’t know what package you use for json. If it is fastjson, you can add parameters to the method of converting json objects. It will always query the subclass attributes. Check the API. Other jars also have similar options
高洛峰2017-04-17 17:59:54
JSONObject jos = JSONObject.fromObject(obj);
JSONArray jo = JSONArray.fromObject(ob);
JAR package net.sf.json
I don’t know if you want to convert like this
大家讲道理2017-04-17 17:59:54
Check out the API of Hibernate's Hibernate.initialize(Class<?> clazz) method. You can find the DEMO on the search engine. I hope it will be helpful to you
PHP中文网2017-04-17 17:59:54
I have also encountered this problem. After querying at the service layer, a list of objects will be returned, which will be converted to Json at the controller layer. This will result in an error: session has been closed. After I found the object list in the service layer, I did the Json conversion, but the object list was still returned.