【hibernate 报错】No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitial...

报错:

1 HTTP Status 500 - Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered 
2 to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.agen.entity.Product_$$_jvst2a_3["handler"]);
3  nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no 
4 properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.agen.entity.Product_$$_jvst2a_3["handler"])
View Code

出现这个问题,是因为:

 1 @ResponseBody
 2     @RequestMapping("/Updateproduct")
 3     @Transactional
 4     public Product updateProduct2(Product product){
 5         Product  product1 = productService.load(product.getProductId());
 6         if(product1 != null){
 7             product1.setProductName(product.getProductName());
 8             product1.setProductCre(product.getProductCre());
 9         }
10         
11         return product1;
12     }
View Code

这个方法中使用的load()获取到数据库中的这一条数据。

使用load()时,进入BUG模式可以看到,虽然获取到这条数据,但是你要看,却发现展示出来的这个对象的字段都是null。

 

但是其中是有值的!!

这样返回给前台,前台接收不到值,会跑出异常:

 

 

修改:

于是我们应该将load()方法修改为get()方法

 1 /**
 2      * 进行产品修改操作
 3      * @return
 4      */
 5     @ResponseBody
 6     @RequestMapping("/Updateproduct")
 7     @Transactional
 8     public Product updateProduct2(Product product){
 9         Product  product1 = productService.get(product.getProductId());
10         if(product1 != null){
11             product1.setProductName(product.getProductName());
12             product1.setProductCre(product.getProductCre());
13         }
14         
15         return product1;
16     }
View Code

 

这样就能解决这个问题!!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值