ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: com.goodfan.entity.BeanA.beanB, no session or session was closed

1. 问题, 当使用JSONArray.fromObject(List<BeanA>)时, beanA中含有BeanB的属性beanB时,会报这个错

2. 解决办法: 使用jsonconfig排除bean类型属性, 有一个排除一个, 嵌套的也一样;

                    JsonConfig jsonConfig = new JsonConfig();
                    jsonConfig.setExcludes(new String[]{"beanB","beanC"});
                    jsonConfig.setIgnoreDefaultExcludes(true);
                    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
                    JSONArray jsonArray = JSONArray.fromObject(beanAList, jsonConfig);

 

posted @ 2016-12-15 16:03  fangfan  阅读(165)  评论(0编辑  收藏  举报