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' ...
随机推荐
- Type.IsContextful 说明
首先看MSDN的说明: 中文:获取一个值,通过该值指示 Type 在上下文中是否可以被承载. 英文:Gets a value indicating whether the Type can be ho ...
- 【ASP.NET实战教程】基于ASP.NET技术下多用户博客系统全程实战开发(NNblog)
岁末主推:牛牛老师主讲,多用户博客系统,基于ASP.NET技术,年后将带来移动业务平台项目项目目标: 打造个性品牌Blogo,定制多用户博客 为每一个博客用户提供个性化的 blogo解决方案,打造精品 ...
- 在WebAPI使用Session
最近在改写WebApp时要将以前用泛型处理例程写的Captcha 改成使用WebApi 来实作机制,在实作的过程中发现使用IRequiresSessionState session也无法使用(cont ...
- 回答阿里社招面试如何准备,顺便谈谈对于Java程序猿学习当中各个阶段的建议
引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的对话都给记下来.LZ自己当初面试完以后,除了记住一些聊过的知识点以外,具体的内容 ...
- c#资料
类型系统: 运行模型: 整型: 浮点: 财务: 布尔: 字符: 引用类型: Console控制字符: {序号,空间:控制字符与精度} 如:{0,3:C2} {空间:#.00} 其中,#表示该位置如果有 ...
- SOA总结(图片打开略慢请知晓)
- [转]Tomcat启动java.lang.OutOfMemoryError: PermGen space错误解决
原文地址:http://outofmemory.cn/java/OutOfMemoryError/outofmemoryerror-permgen-space-in-tomcat-with-eclip ...
- BZOJ 2462: [BeiJing2011]矩阵模板
2462: [BeiJing2011]矩阵模板 Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 915 Solved: 432[Submit][Stat ...
- 【BZOJ-1419】Red is good 概率期望DP
1419: Red is good Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 660 Solved: 257[Submit][Status][Di ...
- Iptables防火墙NAT地址转换与端口转发
开启系统转发功能: [root@localhost /]# vim /etc/sysctl.conf # Generated by iptables-save v1.4.7 on Thu May 12 ...