FetchType.LAZY 时属性加上@JsonIgnore,避免返回时报错:Could not write JSON: failed to lazily initialize a collection of role

【示例】

@OneToMany(fetch=FetchType.LAZY)
@JsonIgnore
@Fetch(FetchMode.SELECT)
@Cascade(value={CascadeType.SAVE_UPDATE})
@JoinColumn(name="userId",updatable=false)
private Set<UserRoleBean> userRoleSet = new HashSet<UserRoleBean>();

----------------------------------------------------------------------------------------------

【说明】

json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响

上述代码userRoleSet 属性上加了  @JsonIgnore,这个时候从后台推数据到前台的时候,就会把userRoleSet 这个引用属性给忽略掉。

避免返回写json时报错:

HTTP Status 500 - Could not write JSON: failed to lazily initialize a collection of role: 

posted on 2016-11-15 19:42  lui  阅读(5240)  评论(0)    收藏  举报