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. Java连接Oracle database小结

    利用jdbc连接Oracle数据库,首先得选对jdbc的版本. jdk6需要ojdbc6.jar这个包.如果选错的话,调用isValid()这个方法时,有可能有这个异常: Exception in t ...

  2. orcle form 传数据乱码

    在jsp顶部加入<%@ page contentType="text/html; charset=utf-8" language="java" impor ...

  3. JavaScript类库---JQuery(二)

    接上: 6.Ajax:  一个基础底层函数:jQuery.ajax();  //高级工具函数都会调用此函数:  一个高级工具方法:load() ;  四个高级工具函数:jQuery.getScript ...

  4. Curl扩展开启失败解决方案

    1.php.ini 开启curl扩展 2.设置有时候开启之后,curl还是不能将php目录下的libssh2.dll复制到apache/bin下.(基本上可以成功) 3. 如果没有开启成功,将php安 ...

  5. django model Meta选项

    可用的 Meta 选项 abstract Options.abstract 如果 abstract = True ,这个 model 就是一个 抽象基类 . app_label Options.app ...

  6. 使用 antd Table组件, 异步获取数据

    使用React.js + Redux + antd 制作CMS 后台内容管理系统,分享一点点积累,欢迎讨论. 在this.state中初始化数据: this.state = { pageNum:1, ...

  7. Android只能动态注册的广播Action

    只能动态注册的广播(部分): android.intent.action.SCREEN_ON android.intent.action.SCREEN_OFF android.intent.actio ...

  8. 看jpg和png图片

    emacs 24.4 下载http://pan.baidu.com/s/1mgIEPHe里的: zlib1.dll, libpng16-16.dll(png)和libjpeg-9.dll到emacs里 ...

  9. Python笔记(3)迭代器与生成器

    参考自:http://www.cnblogs.com/huxi/category/251137.html 迭代器 迭代器是访问集合内元素的一种方式,他不能倒退只能一直迭代下去.可以写到for循环in后 ...

  10. UIView的setNeedsDisplay和setNeedsLayout

    1,UIView的setNeedsDisplay和setNeedsLayout方法 首先两个方法都是异步执行的.而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到  UI ...