我正在使用 Spring Framework v3.2.4 和 JPA + Hibernate 4 实现 RESTful 服务。我以 JSON 格式返回资源(使用 Jackson Mapper),但现在我遇到了这个异常:
Could not write JSON: failed to lazily initialize a collection of role: it.teck.service.model.Canvas.params, could not initialize proxy - no Session (through reference chain: it.teck.service.model.Canvas["params"]);
Canvas
我在和实体之间有“多对多”关系,Param
当向服务请求画布时,我还需要序列化参数列表。
在我的课上,我有:
@Entity
public class Canvass {
@ManyToMany
@JoinTable(name = "canvas_params", joinColumns = { @JoinColumn(name = "id_canvas", referencedColumnName = "id_canvas") }, inverseJoinColumns = { @JoinColumn(name = "id_param", referencedColumnName = "id_param") })
private List<Param> params;
// ...
}
和:
@Entity
public class Param {
@ManyToMany(mappedBy = "params")
private List<Canvas> canvasList;
// ...
}
在一些帖子和 SO 答案中,建议从序列化中排除ManyToMany
字段,以打破序列化循环,但我需要序列化链接到我的画布实体的参数。所以我该怎么做?