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

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

  1. Winform下CefSharp的引用、配置、实例与报错排除(源码)

    Winform下CefSharp的引用.配置.实例与报错排除 本文详细介绍了CefSharp在vs2013..net4.0环境下,创建Winfrom项目.引用CefSharp的方法,演示了winfro ...

  2. Spring:解决因@Async引起的循环依赖报错

    最近项目中使用@Async注解在方法上引起了循环依赖报错: org.springframework.beans.factory.BeanCurrentlyInCreationException: Er ...

  3. Spring中解决循环依赖报错的问题

    什么是循环依赖 当一个ClassA依赖于ClassB,然后ClassB又反过来依赖ClassA,这就形成了一个循环依赖: ClassA -> ClassB -> ClassA 原创声明 本 ...

  4. 谁说java里面有返回值的方法必须要有返回值,不然会报错????

    慢慢的总是发现以前的学得时候有些老师讲的不对的地方! 所以还是尽量别把一些东西说的那么绝对,不然总是很容易误导别人,特别是一些你自己根本就没有试过的东西,然后又斩钉截铁的告诉别人,这样不行,肯定不行什 ...

  5. 引用HM.Util.Ioc 的时候报错

    引用HM.Util.Ioc 的时候报错 错误:The type name or alias SqlServer could not be resolved. Please check your con ...

  6. 使用spring jpa 时,利用nativeQuery,获取数据,无需新建实体,按照别名返回Json数据

    刚开始是这样写的 @Query(value = "SELECT ll.user_id id ,u.catong_img catong_img,ll.locationId location_i ...

  7. spring boot 2 全局统一返回RESTful风格数据、统一异常处理

    全局统一返回RESTful风格数据,主要是实现ResponseBodyAdvice接口的方法,对返回值在输出之前进行修改.使用注解@RestControllerAdvice拦截异常并统一处理. 开发环 ...

  8. spring源码之bean的初始化及循环引用

    实例化方法,把bean实例化,并且包装成BeanWrapper 1.点进这个方法里面. 这个方法是反射调用类中的 factoryMethod 方法. 这要知道@Bean 方法的原理, 实际上sprin ...

  9. 【spring data jpa】使用jpa的@Query,自己写的语句,报错:org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' cannot be found on null

    报错: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' ...

随机推荐

  1. 微信小程序入门正确姿势(一)

    [未经作者本人同意,请勿以任何形式转载] >>>前言 这是 [认真学编程] 系列的 第4篇 文章(微信小程序入门系列),欢迎点赞分享.写留言,这些都是对我最好的支持. 本系列适合有一 ...

  2. [Django 1]安装Django并创建虚拟虚拟环境项目

    1)安装Django 使用pip来安装,命令如下: pip3 install Djangopip install Django(安装到python2)python3 -m pip install Dj ...

  3. 51Nod-1136 欧拉函数

    51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1136 1136 欧拉函数 基准时间限制:1 秒 空间限制: ...

  4. .Net Core Linux centos7行—.net core json 配置文件

    .net core 对配置系统做出了大幅度更新,不在局限于之前的*.xml配置方式.现在支持json,xml,ini,in memory,环境变量等等.毫无疑问的是,现在的json配置文件是.net ...

  5. c# WebClient Get Post 方法

    public string GetData(string url) { string data; using (var client = new WebClient()) { using (var s ...

  6. 下载imagenet2012数据集

    摸索了一下,imagenet2012下载,跟大家分享一下 用迅雷会员加速都可以下载,有的用百度云也可以离线下载 http://www.image-net.org/challenges/LSVRC/20 ...

  7. oneuijs/You-Dont-Need-jQuery

    oneuijs/You-Dont-Need-jQuery  https://github.com/oneuijs/You-Dont-Need-jQuery/blob/master/README.zh- ...

  8. 搭建maven项目

    一.新建项目 点击"Maven Project",如果没有的话在"Other"里面 勾选这个选项,这样可以建立一个没有多余文件的简单项目(如果这个时候直接建立w ...

  9. python取mysql数据写入excel

    环境:MySQLdb openpyxl模块 python去zabbix的mysql数据库中取交换机不同时间段的进出口流量,然后写入excel中,每天cron执行,每周四邮件发送.(代码中第一行必须加上 ...

  10. Java编程中的美好

    java程序员如何写出"优美"代码,动力节点告诉你怎么办: 1.注释尽可能全面 对于方法的注释应该包含详细的入参和结果说明,有异常抛出的情况也要详细叙述:类的注释应该包含类的功能说 ...