博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring jpa 实体互相引用返回restful数据循环引用报错的问题
阅读量:4313 次
发布时间:2019-06-06

本文共 3550 字,大约阅读时间需要 11 分钟。

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
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
getLabel() { return labeles; } public void setLabel(List
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数据时递归调用的问题。

转载于:https://www.cnblogs.com/rwxwsblog/p/5853754.html

你可能感兴趣的文章
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>
MapReduce的 Speculative Execution机制
查看>>
大数据学习之路------借助HDP SANDBOX开始学习
查看>>
Hadoop基础学习:基于Hortonworks HDP
查看>>
为什么linux安装程序 都要放到/usr/local目录下
查看>>
Hive安装前扫盲之Derby和Metastore
查看>>
永久修改PATH环境变量的几种办法
查看>>
大数据学习之HDP SANDBOX开始学习
查看>>
Hive Beeline使用
查看>>
Centos6安装图形界面(hdp不需要,hdp直接从github上下载数据即可)
查看>>
CentOS7 中把yum源更换成163源
查看>>
关于yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
查看>>
2020-11-18
查看>>
Docker面试题(二)
查看>>