ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何修复弹簧靴一对多双向无限循环?

2019-10-13 07:20:21  阅读:290  来源: 互联网

标签:spring spring-boot spring-data hibernate-mapping bidirectional


我尝试使用spring boot和spring data jpa创建一对多的双向映射,请查看以下实体

Employer Entity

@Entity  
public class Employer  
{  
private Long id;  
private String employerName;  
private List<Employee> employees;  

@Id  
@GeneratedValue(strategy=GenerationType.AUTO)  
public Long getId()  
{  
    return id;  
}  
public void setId(Long id)  
{  
    this.id = id;  
}  
public String getEmployerName()  
{  
    return employerName;  
}  
public void setEmployerName(String employerName)  
{  
    this.employerName = employerName;  
}  

@OneToMany(cascade=CascadeType.ALL, mappedBy="employer")  
public List<Employee> getEmployees()  
{  
    return employees;  
}  
public void setEmployees(List<Employee> employees)  
{  
    this.employees = employees;  
}  
} 

Employee Entity

@Entity  
public class Employee  
{  
private Long id;  
private String employeeName;  
private Employer employer;  

@Id  
@GeneratedValue(strategy=GenerationType.AUTO)  
public Long getId()  
{  
    return id;  
}  
public void setId(Long id)  
{  
    this.id = id;  
}  
public String getEmployeeName()  
{  
    return employeeName;  
}  
public void setEmployeeName(String employeeName)  
{  
    this.employeeName = employeeName;  
}  
@ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)  
public Employer getEmployer()  
{  
    return employer;  
}  
public void setEmployer(Employer employer)  
{  
    this.employer = employer;  
}  
}  

Employer Repo

public interface EmployerServices extends JpaRepository<Employer, Long> {
}

Employee Repo

public interface EmployeeServices extends JpaRepository<Employee, Long> {
}

REST Controller is

 @RestController
 public class Controller {
 @Autowired EmployeeServices employeeServices;
 @Autowired EmployerServices employerServices;
 @GetMapping("/getempr")
 public Object getempr(){
    return employerServices.findOne(1L);
 }
}

现在问题开始开始我的输出
enter image description here

它看起来像是内斗循环,并且我的服务器抛出错误getOutputStream()已为此响应调用.

 I used @JsonBackReference & @JsonManagedReference 

注解,但问题是它的工作方式像一对多

 {
   "id":1,
   "employerName":"employer",
   "employees":[
     {"id":1,"employeeName":"emp1"},
     {"id":2,"employeeName":"emp2"}
   ]
}  

如果我想引起许多雇主的关注,就像所有雇员一样.输出是

 [
  {
   "id":1,
   "employeeName":"emp1"
  },
  {
    "id":2,
    "employeeName":"emp2"}
 ]

它没有向我显示雇主的详细信息.

请向我建议我在做什么错.提前致谢!!

解决方法:

使用JSON,则是双向映射的问题.使用以下属性.

@JsonIgnoreProperties("employer")
@JsonIgnoreProperties("employees")

请保持获取类型的热情.

希望这会起作用.

标签:spring,spring-boot,spring-data,hibernate-mapping,bidirectional
来源: https://codeday.me/bug/20191013/1906207.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有