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数据时递归调用的问题。
spring jpa 实体互相引用返回restful数据循环引用报错的问题的更多相关文章
- Winform下CefSharp的引用、配置、实例与报错排除(源码)
Winform下CefSharp的引用.配置.实例与报错排除 本文详细介绍了CefSharp在vs2013..net4.0环境下,创建Winfrom项目.引用CefSharp的方法,演示了winfro ...
- Spring:解决因@Async引起的循环依赖报错
最近项目中使用@Async注解在方法上引起了循环依赖报错: org.springframework.beans.factory.BeanCurrentlyInCreationException: Er ...
- Spring中解决循环依赖报错的问题
什么是循环依赖 当一个ClassA依赖于ClassB,然后ClassB又反过来依赖ClassA,这就形成了一个循环依赖: ClassA -> ClassB -> ClassA 原创声明 本 ...
- 谁说java里面有返回值的方法必须要有返回值,不然会报错????
慢慢的总是发现以前的学得时候有些老师讲的不对的地方! 所以还是尽量别把一些东西说的那么绝对,不然总是很容易误导别人,特别是一些你自己根本就没有试过的东西,然后又斩钉截铁的告诉别人,这样不行,肯定不行什 ...
- 引用HM.Util.Ioc 的时候报错
引用HM.Util.Ioc 的时候报错 错误:The type name or alias SqlServer could not be resolved. Please check your con ...
- 使用spring jpa 时,利用nativeQuery,获取数据,无需新建实体,按照别名返回Json数据
刚开始是这样写的 @Query(value = "SELECT ll.user_id id ,u.catong_img catong_img,ll.locationId location_i ...
- spring boot 2 全局统一返回RESTful风格数据、统一异常处理
全局统一返回RESTful风格数据,主要是实现ResponseBodyAdvice接口的方法,对返回值在输出之前进行修改.使用注解@RestControllerAdvice拦截异常并统一处理. 开发环 ...
- spring源码之bean的初始化及循环引用
实例化方法,把bean实例化,并且包装成BeanWrapper 1.点进这个方法里面. 这个方法是反射调用类中的 factoryMethod 方法. 这要知道@Bean 方法的原理, 实际上sprin ...
- 【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' ...
随机推荐
- java中的tuple实现
java中没有类似c#.scala中的tuple元组类,只能自己动手,丰衣足食了,见下面的代码: Tuple 抽象类 import java.util.Optional; /** * Tuple元组类 ...
- java并发编程学习: 守护线程(Daemon Thread)
在正式理解这个概念前,先把 守护线程 与 守护进程 这二个极其相似的说法区分开,守护进程通常是为了防止某些应用因各种意外原因退出,而在后台独立运行的系统服务或应用程序. 比如:我们开发了一个邮件发送程 ...
- JS禁止选中文本方法
if (typeof(element.onselectstart) != "undefined") { // IE下禁止元素被选取 element.onselectstart = ...
- Moto C118 基于 Osmocom-BB 和 OpenBTS 搭建小型GSM短信基站
此文章PDF文档下载地址:点击下载 0x00 写在前面 大家应该都听说过摩托罗拉C118配合Osmocom-BB实现GSM网络下的短信拦截功能吧,在14年左右新出了一种玩法就是Osmocom-BB的s ...
- IoC模式(依赖、依赖倒置、依赖注入、控制反转)
1.依赖 依赖就是有联系,有地方使用到它就是有依赖它,一个系统不可能完全避免依赖.如果你的一个类或者模块在项目中没有用到它,恭喜你,可以从项目中剔除它或者排除它了,因为没有一个地方会依赖它.下面看一个 ...
- CCF 201604-2 俄罗斯方块
题目不难,但是感觉很有意思.一开始忘了把调试信息注释掉,WA了两次... 试题编号: 201604-2 试题名称: 俄罗斯方块 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 ...
- Sqlserver 语法总结
修改列类型 alter table PRO_Element_b alter column matname varchar(1024) 更改一个表中的数据到另外一个表中 update a set a.n ...
- 创建django项目
python /usr/local/lib/python3.4/dist-packages/Django-1.9.10-py3.4.egg/django/bin/django-admin.py sta ...
- 在渲染前获取 View 的宽高
在渲染前获取 View 的宽高 这是一个比较有意义的问题,或者说有难度的问题,问题的背景为:有时候我们需要在view渲染前去获取其宽高,典型的情形是,我们想在onCreate.onStart.onRe ...
- nginx 反向代理
nginx 反向代理 vim nginx.conf http { ..... upstream "tomcatweb" { server 172.30.13.199:8080; s ...