【hibernate 報錯】No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer 【get和load的區別】


報錯:

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

 

這樣就能解決這個問題!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM