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

时间:2024-09-25 14:35:38

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);