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数据时递归调用的问题。








本文转自秋楓博客园博客,原文链接:http://www.cnblogs.com/rwxwsblog/p/5853754.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值