Hibernate懒加载导致json数据对象传输异常的问题---(非常重要)
1. 异常:
[console_demo][WARN] [2016-12-15 19:49:35] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable(407) | Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could
not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain:
java.util.HashMap["ckRequirePartLists"]->java.util.ArrayList[0]->com.dyoon.cmms.pojo.CkRequirePartList["ckPurchaseBill"]->com.dyoon.cmms.pojo.CkPurchaseBill["applyOperator"]->com.dyoon.cmms.pojo.UserOperator_
_jvsta2a_8["userModifyLog"]->com.dyoon.cmms.pojo.UserModifyLog_
nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
(through reference chain: java.util.HashMap["ckRequirePartLists"]->java.util.ArrayList[0]->com.dyoon.cmms.pojo.CkRequirePartList["ckPurchaseBill"]->com.dyoon.cmms.pojo.CkPurchaseBill["applyOperator"]->com.dyoon.cmms.pojo.UserOperator__jvsta2a_6["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.HashMap["ckRequirePartLists"]->java.util.ArrayList[0]->com.dyoon.cmms.pojo.CkRequirePartList["ckPurchaseBill"]->com.dyoon.cmms.pojo.CkPurchaseBill["applyOperator"]->com.dyoon.cmms.pojo.UserOperator_
_jvsta2a_12["sysRole"]->com.dyoon.cmms.pojo.SysRole_
_jvsta2a_6["handler"])
2. 原因:
使用注解@ResponseBody返回由Hibernate DAO直接获得的pojo对象内存在Lazy关联对象,执行Jason转换时,会从数据库加载该对象,而此时session已关闭,导致加载失败,进而导致Jason转换失败。
3 解决:
方法一:在不影响数据完整性的情况下,使用set方法手动置空pojo类的Lazy关联对象。(这样如果关联的对象比较多,可能漏的。)
方法二:使用@JsonIgnore注释掉pojo类的Lazy关联对象。(这种如果在其他的地方需要用关联的对象转json,这样就会导致失效。)
方法三:自定义类,二次封装所需的pojo对象的属性。
4. 事例:以下对第三种方法做展示
jsp页面:
-
function showPartDetails(purchBillId){
-
-
$.ajax({
-
type:"post",
-
url:"${rootPath }/depot/purchase/getPartByPurchBillId?purchBillId="+purchBillId,
-
success : function(data){
-
alert(data.requirePartListBeans[0].partName);
-
}
-
});
-
}
通过ajax发送请求到controller,服务器处理结果,将结果以json的格式发送给前端。
常规下的controller如下:
-
@RequestMapping(value = "/getPartByPurchBillId")
-
@ResponseBody
-
public Map<String,Object> getPartByPurchBillId(@RequestParam(value="purchBillId") Integer purchBillId) throws JsonProcessingException {
-
Map<String, Object> map = new HashMap<>();
-
List<CkRequirePartList> ckRequirePartLists = purchaseService.getPartByPurchBillId(purchBillId);
-
map.put("requirePartListBeans", ckRequirePartLists);
-
return map;
-
}
但是由于hibernate懒加载的问题,requirePartListBeans不能正常的转换为json,这时候将前端要获取的属性数据封装成一个类,以下为假如前端要获取的3条数据:
-
public class RequirePartListBean {
-
-
private String partNumber;
-
private String partName;
-
private BigDecimal partQuantity;
-
-
public String getPartNumber() {
-
return partNumber;
-
}
-
-
public void setPartNumber(String partNumber) {
-
this.partNumber = partNumber;
-
}
-
-
public String getPartName() {
-
return partName;
-
}
-
-
public void setPartName(String partName) {
-
this.partName = partName;
-
}
-
-
public BigDecimal getPartQuantity() {
-
return partQuantity;
-
}
-
-
public void setPartQuantity(BigDecimal partQuantity) {
-
this.partQuantity = partQuantity;
-
}
-
}
更改后的controller为:
-
@RequestMapping(value = "/getPartByPurchBillId")
-
@ResponseBody
-
public Map<String,Object> getPartByPurchBillId(@RequestParam(value="purchBillId") Integer purchBillId) throws JsonProcessingException {
-
Map<String, Object> map = new HashMap<>();
-
List<CkRequirePartList> ckRequirePartLists = purchaseService.getPartByPurchBillId(purchBillId);
-
RequirePartListBean r = null;
-
List<RequirePartListBean> requirePartListBeans = new ArrayList<>();
-
for (CkRequirePartList ckRequirePartList : ckRequirePartLists) {
-
r = new RequirePartListBean();
-
r.setPartName(ckRequirePartList.getModPartCatalog().getPartName());
-
r.setPartNumber(ckRequirePartList.getModPartCatalog().getPartNumber());
-
r.setPartQuantity(ckRequirePartList.getPartQuantity());
-
requirePartListBeans.add(r);
-
}
-
map.put("requirePartListBeans", requirePartListBeans);
-
return map;
-
}
前端ajax通过success响应数据,
-
success : function(data){
-
alert(data.requirePartListBeans[0].partName);
-
}
至此,问题得到解决。还有问题的可以留言。
原文地址:https://blog.csdn.net/baidu_28283827/article/details/53675121
Hibernate懒加载导致json数据对象传输异常的问题---(非常重要)的更多相关文章
- hibernate懒加载和json序列化冲突
因为懒加载这个对象属性只是一个代理对象,如果json直接当作一个存在的属性去序列化就会出现错误,所以就只能这样了,当然还有其他办法吧 或者在class上加上 @JsonIgnoreProperties ...
- hibernate懒加载导致jackjson解析json时StackOverFlow
@JsonIgnore @JsonFilter @JsonBackReference @JsonManagedReference @JsonIgnoreProperties jackson中的@Jso ...
- -java转json hibernate懒加载造成的无限递归问题
1.在判断到底是谁维护关联关系时,可以通过查看外键,哪个实体类定义了外键,哪个类就负责维护关联关系. JoinColumn(name="pid") 2. 在保存数据时,总是先保存的 ...
- hibernate懒加载(转载)
http://blog.csdn.net/sanjy523892105/article/details/7071139 懒加载详解 懒加载为Hibernate中比较常用的特性之一,下面我们详细来了解下 ...
- ssh中Hibernate懒加载,session问题的学习与理解
交代本项目中要求获取session的方式如下: public Session getCurrentSession() { // 增删改使用的session,事务必须是开启的(Required,即pro ...
- Hibernate懒加载深入分析
Hibernate懒加载深入分析 懒加载可以提高性能吗? 不可以简单的说"能",因为Hibernate的关系映射拖累了SQL的性能,所以想出懒加载来弥补.只是弥补而以,不会超越. ...
- web进修之—Hibernate 懒加载(6)
关于懒加载 在关系数据库设计的时候,我们很多时候把表之间的关系设置为强关联(使用外键进行约束),在Hibernate中利用对象的包含关系进行维护(HIbernate本身就是面向对象的数据库操作模式), ...
- Hibernate懒加载解析
Hibernate懒加载解析 在Hibernate框架中,当我们要访问的数据量过大时,明显用缓存不太合适, 因为内存容量有限 ,为了减少并发量,减少系统资源的消耗,这时Hibernate用懒加载机制来 ...
- hibernate懒加载
Hibernate懒加载解析 hibernatejoinsession数据库sqlobject Hibernate懒加载解析 在Hibernate框架中,当我们要访问的数据量过大时,明显用缓存不太合适 ...
随机推荐
- Codefroces 213E. Two Permutations
E. Two Permutations time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- expect.js
前言 1> 借鉴里面的应用思想,使用断言提高代码的健壮性及维护性 2> 实现方式--不采用直接嵌入expect的方式,统一进行重写(提取常用断言方法,重新构造API) 官网介绍 https ...
- loj #10001. 「一本通 1.1 例 2」种树
题面 解题思路 贪心,首先按右端点排序,然后从小往大扫,因为要求树最少,所以要尽量放在右端点.然后开个bool数组判断是否种过树即可. 代码 #include<iostream> #inc ...
- bzoj 2705 [SDOI2012]Longge的问题——欧拉函数大水题
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705 撕逼题.不就是枚举gcd==d,求和phi[ n/d ]么. 然后预处理sqrt (n ...
- QT_string转char*
char* convertQString2char(const QString &str) { QByteArray ba = str.toUtf8(); char * pathChar = ...
- Python re.sub函数
- IE8下的兼容小经验
placeholder IE8下不支持HTML5属性placeholder,不过为解决此问题的js插件挺多的,比如:jquery-placeholder.也可以使用jquery来写. last-chi ...
- PHP学习(类型转化)
PHP 在变量定义中不需要(或不支持)明确的类型定义:变量类型是根据使用该变量的上下文所决定的.也就是说,如果把一个 string 值赋给变量 $var , $var 就成了一个 string .如果 ...
- 【JZOJ4810】【NOIP2016提高A组五校联考1】道路规划
题目描述 输入 输出 样例输入 5 1 4 5 2 3 3 4 2 1 5 样例输出 3 数据范围 样例解释 解法 模型显然. 设第一列为a[],第二列为b[],f[i]为前i个数的最大答案. 顺序枚 ...
- Docker Remote API使用准备
1 修改配置文件 CentOS: /etc/sysconfig/docker Ubuntu: /etc/init/docker.conf 1.添加: DOCKER_OPTS='-H tcp://0.0 ...