这是参考中的我一个月前问的一个问题 https://stackoverflow.com/questions/13354766/avoid-hibernate-lazy-initialization-exception.
在这个问题中 https://stackoverflow.com/questions/13354766/avoid-hibernate-lazy-initialization-exception避免 json 序列化时出现惰性初始化异常的答案是将导致惰性初始化异常的变量设置为 null。但请考虑当类有很多依赖项时。现在随着代码库的增长,每次我都必须将代码中各处的麻烦变量设置为 null 以避免 json 序列化问题。当代码库很大时,该方法看起来不太整洁。
下面显示的示例代码看起来不太好。
//setting some variables to avoid lazy init exception in jackson mapper serialization
batch.setEnrollmentList(null);
List<BatchSchedule> scheduleList = (ArrayList<BatchSchedule>) batch.getBatchScheduleList();
for (BatchSchedule batchSchedule : scheduleList) {
batchSchedule.setBatch(null);
}
batch.getLecturer().setBatchList(null);
batch.getLecturer().setSubjectList(null);
batch.getSubject().setBatchList(null);
batch.getSubject().setLecturerList(null);
您能给我建议一个更好的方法来处理这个问题吗?
谢谢。
您可以使用以下方式注释惰性属性@JsonIgnore
以至于杰克逊在连载时忽略了它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)