我有兩個表部門和員工。 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。如果傳遞給傑克遜的對象包含循環引用,則會遇到這種行爲 –