spring jpa 实体互相引用返回restful数据循环引用报错的问题

简介: spring jpa 实体互相引用返回restful数据循环引用报错的问题Java实体里两个对象有关联关系,互相引用,比如,在一对多的关联关系里Problem对象,引用了标签列表ProblemLabelProblemLabel对象,引用了所属Problem这样构成了互相引用,导致递归循环内存溢出异常:org.

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

相关文章
|
12天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
22 0
|
14天前
|
SQL Java 数据库连接
Springboot框架整合Spring JDBC操作数据
JDBC是Java数据库连接API,用于执行SQL并访问多种关系数据库。它包括一系列Java类和接口,用于建立数据库连接、创建数据库操作对象、定义SQL语句、执行操作并处理结果集。直接使用JDBC涉及七个步骤,包括加载驱动、建立连接、创建对象、定义SQL、执行操作、处理结果和关闭资源。Spring Boot的`spring-boot-starter-jdbc`简化了这些步骤,提供了一个在Spring生态中更便捷使用JDBC的封装。集成Spring JDBC需要添加相关依赖,配置数据库连接信息,并通过JdbcTemplate进行数据库操作,如插入、更新、删除和查询。
|
14天前
|
SQL Java 数据库连接
Springboot框架整合Spring Data JPA操作数据
Spring Data JPA是Spring基于ORM和JPA规范封装的框架,简化了数据库操作,提供增删改查等接口,并可通过方法名自动生成查询。集成到Spring Boot需添加相关依赖并配置数据库连接和JPA设置。基础用法包括定义实体类和Repository接口,通过Repository接口可直接进行数据操作。此外,JPA支持关键字查询,如通过`findByAuthor`自动转换为SQL的`WHERE author=?`查询。
|
14天前
|
Java 测试技术 Maven
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
|
1月前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
23 3
|
1月前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
33 3
|
1月前
|
前端开发 Java API
头头是道:揭示Spring MVC如何获取和处理请求头数据
头头是道:揭示Spring MVC如何获取和处理请求头数据
26 1
|
1月前
|
前端开发 Java API
饼干探秘:深入Spring MVC中获取Cookie数据的技术解析
饼干探秘:深入Spring MVC中获取Cookie数据的技术解析
18 3
|
2月前
|
Java Apache vr&ar
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
16 0
|
2月前
|
Java Windows Perl
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
17 0