Could not write JSON: failed to lazily initialize a collection of role: com.blog.Entity.UserInfo.userRoles, could not initialize proxy - no Session;
nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.blog.yongyu.signInStart.Entity.UserInfo.userRoles, could not initialize proxy - no Session (through reference chain: com.blog..Entity.BaseClass.DataResult[\"data\"]->java.util.ArrayList[0]->com.blog.Entity.UserInfo[\"role\"])",
postman发送请求时,报错信息如上。
原因在于:类User Info和Role使用OneToMany时,类UserInfo中用了懒加载fetch=FetchType.LAZY,导致在类UserInfo中取List<Role>时,加载数据的session已经关闭,出现报错。
解决方法:
别人的答案:JPA中的failed to lazily initialize a collection of role:xxxx no session or session was closed异常分析与解决
jap使用时遇到failed to lazily initialize a collection of XXXX 问题
原文:https://www.cnblogs.com/liuyongyu/p/10925140.html