我有两个表部门和员工。 Department是Employee的父级,两者都由department_id加入。Spring-jpa中的关系无限递归
部
@Entity
public class Department {
@Override
public String toString() {
return "Department [departmentId=" + departmentId + ", departmentName=" + departmentName + "]";
}
@Id
public Integer departmentId;
public String departmentName;
@OneToMany(mappedBy = "department",fetch=FetchType.LAZY)
public Set<Employee> employees;
}
员工
@Entity
public class Employee {
@Override
public String toString() {
return "Employee [employee_id=" + employeeId + ", employee_name=" + employeeName + ", department="
+ department + "]";
}
@Id
public Integer employeeId;
public String employeeName;
@ManyToOne
@JoinColumn(name = "departmentId")
public Department department;
}
当我试图检索所有部门详细它是创造一个循环依赖和杰克逊抛出下面的错误。 所以我想知道如何处理这种情况,您需要灵活地从父级访问子级详细信息并从子级访问父级详细信息。
2017-08-01 12:03:13.245 WARN 18197 --- [o-auto-1-exec-3]
.w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message:
org.springframework.http.converter.HttpMessageNotWritableException: Could not
write JSON: Infinite recursion (StackOverflowError); nested exception is
com.fasterxml.jackson.databind.JsonMappingException:
它无关休眠/弹簧JPA。如果传递给杰克逊的对象包含循环引用,则会遇到这种行为 –