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數據時遞歸調用的問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM