spring jpa 实体互相引用返回restful数据循环引用报错的问题
spring jpa 实体互相引用返回restful数据循环引用报错的问题
Java实体里两个对象有关联关系,互相引用,比如,在一对多的关联关系里
Problem
对象,引用了标签列表ProblemLabel
ProblemLabel
对象,引用了所属Problem
这样构成了互相引用,导致递归循环内存溢出异常:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError) (through reference chain: com.test.api.problem.domain.ProblemLabel["problem"]->com.test.api.problem.domain.Problem["label"]->org.hibernate.collection.internal.PersistentBag[0]->com.test.api.problem.domain.ProblemLabel["problem"]->com.test.api.problem.domain.Problem["label"]->org.hibernate.collection.internal.PersistentBag[0]->com.test.api.problem.domain.ProblemLabel["problem"]
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "tx_test_problem")
public class Problem {
private static final long serialVersionUID = 761718569700121659L;
/**
* 问题概述
*/
private String qutline;
/**
* 问题补充
*/
private String supplement;
/**
* 创建时间
*/
private Date createDate;
private Account user;
private List<ProblemLabel> labeles;
public String getQutline() {
return qutline;
}
public void setQutline(String qutline) {
this.qutline = qutline;
}
public String getSupplement() {
return supplement;
}
public void setSupplement(String supplement) {
this.supplement = supplement;
}
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
public Account getUser() {
return user;
}
public void setUser(Account user) {
this.user = user;
}
@Column(updatable = false)
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@OneToMany(mappedBy = "problem", fetch = FetchType.EAGER) //主表上添加mappedBy,指向关联表的关联实体problem即可
public List<ProblemLabel> getLabel() {
return labeles;
}
public void setLabel(List<ProblemLabel> labeles) {
this.labeles = labeles;
}
}
Problem包含了标签列表private List<ProblemLabel> labeles;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "tx_test_problem_label")
public class ProblemLabel {
private static final long serialVersionUID = -789585899105406906L;
private String labelVal;
private String problemId;
private Problem problem;
@ManyToOne
@JoinColumn(name = "problem_id")
public Problem getProblem() {
return problem;
}
public void setProblem(Problem problem) {
this.problem = problem;
}
public String getLabelVal() {
return labelVal;
}
public void setLabelVal(String labelVal) {
this.labelVal = labelVal;
}
}
ProblemLabel包含了标签列表private Problem problem;
在不需要展现一方的属性上添加忽略注解@JsonIgnore
即可。修改后的ProblemLabel
如下。
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "tx_test_problem_label")
public class ProblemLabel {
private static final long serialVersionUID = -789585899105406906L;
private String labelVal;
private String problemId;
private Problem problem;
@ManyToOne
@JoinColumn(name = "problem_id")
@JsonIgnore //将不需要返回的属性上添加忽略
public Problem getProblem() {
return problem;
}
public void setProblem(Problem problem) {
this.problem = problem;
}
public String getLabelVal() {
return labelVal;
}
public void setLabelVal(String labelVal) {
this.labelVal = labelVal;
}
}
这样便可解决bean互相引用返回json数据时递归调用的问题。
扫描公众号,关注更多信息
---------------------------------------------------------------------------------我是分割线--------------------------------------------------------------------------to be a better me, talk is cheap show me the code
版权所有,转载请注明原文链接。
文中有不妥或者错误的地方还望指出,以免误人子弟。如果觉得本文对你有所帮助不妨【推荐】一下!如果你有更好的建议,可以给我留言讨论,共同进步!
再次感谢您耐心的读完本篇文章。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?