问题描述
我尝试使用spring boot和spring data jpa创建一对多的双向映射,请查看以下实体
i am try to create a one to many bidirectional mapping using spring boot and spring data jpa please look the below 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;
}
}
@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;
}
}
public interface EmployerServices extends JpaRepository<Employer, Long> {
}
public interface EmployeeServices extends JpaRepository<Employee, Long> {
}
@RestController
public class Controller {
@Autowired EmployeeServices employeeServices;
@Autowired EmployerServices employerServices;
@GetMapping("/getempr")
public Object getempr(){
return employerServices.findOne(1L);
}
}
现在问题开始开始,请看我的输出
now the problem begin start see my out put
它看起来像是内斗循环,我的服务器抛出错误getOutputStream()已为此响应调用.
its look like a infighting loop and my server throwing error getOutputStream() has already been called for this response.
I used @JsonBackReference & @JsonManagedReference
注释,但问题在于它的作用就像一对多
annotation but the problem is its working like one to many
{
"id":1,
"employerName":"employer",
"employees":[
{"id":1,"employeeName":"emp1"},
{"id":2,"employeeName":"emp2"}
]
}
如果我想引起所有人之间的关注,就像所有有雇主的雇员一样.输出是
if I am trying to get in the concern of many to one like all employee with employer. the output is
[
{
"id":1,
"employeeName":"emp1"
},
{
"id":2,
"employeeName":"emp2"}
]
它没有向我显示雇主详细信息.
its not showing me the employer details.
请向我建议我在做什么错.预先感谢!
please suggets me guys what i am doing wrong. thanks in advance!!
推荐答案
使用JSON是双向映射的问题.使用以下属性.
with the JSON its a problem with bi-directional mapping. Use the below properties.
@JsonIgnoreProperties("employer")
@JsonIgnoreProperties("employees")
请始终保持获取类型.
希望这会起作用.
这篇关于如何修复弹簧靴一对多双向无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!