spring jpa 实体互相引用返回restful数据循环引用报错的问题
2016-09-08 17:09
344 查看
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循环引用的问题
- spring 返回 JSON乱码问题的解决 及个人 动态加载ztree树结构的json数据代码
- 解决spring mvc +hibernate整合时使用jackson返回json数据无限循环问题的官方解决方案
- spring boot新手教程之使用FastJson解析JSON数据以及解决返回中文乱码问题
- ADO.NET 实体数据模型 异常-“序列化类型为 XX 的对象时检测到循环引用”
- block的循环引用问题1(ARC和MRC下返回block)
- 【JSON 注解】JSON循环引用2----JSON注解@JsonIgnoreProperties+JAVA关键字transient+后台对象与JSON数据的格式互相转化
- shiro+spring boot+mybatis启动循环引用问题解决思路和方案
- Spring Data Jpa中实体主键问题
- 关于EDM模型中多个实体之间循环引用导致保存数据失败的解决方案一例
- bootstrap table处理含有子集json的数据以及fastjson禁止循环引用的问题
- Spring循环引用和AOP代理引发的两个问题
- cxf 与spring集成,发布restful接口,返回json数据
- ADO.NET 实体数据模型 异常-“序列化类型为 XX 的对象时检测到循环引用”
- Spring mvc+jackson+jquery easyui返回json数据出现整合出问题
- Spring mvc+jackson+jquery 返回json数据出现整合出现包冲突问题
- Swift自定义数据模型及闭包的循环引用问题
- Spring boot jpa 删除数据和事务管理的问题实例详解
- spring jpa CrudRepository save 新建数据没有返回id
- spring 使用一般接口 返回数据null 的问题 解决