我有一个使用One-To-One和One-To-Many关系的中型项目。几天前,我在一对多关系中获得了无限递归,但我设法使用这两个注释来修复它: @JsonManagedReference 和 @JsonBackReference < /强>
现在我面临同样的错误,但是在一对一关系中。并且它不在&#34;几个领域&#34;但只是在一个。让我发布一些代码。我有一个Employee和一个Task类。一个员工当时有一个任务。所以:
Employee.java
@Entity
@Table(name = "employee")
public class Employee {
int id;
@NotNull
String firstName;
private Task taskRefference;
public Employee() {
}
public Employee(firstName) {
this.firstName = firstName;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "task_id")
public Task getTaskRefference() {
return taskRefference;
}
public void setTaskRefference(Task taskRefference) {
this.taskRefference = taskRefference;
}
Task.java
@Entity
@Table(name = "task")
public class Task {
int id;
@NotNull
String title;
@NotNull
String assignees;
private Employee employee;
public Task(String title, String assignees) {
this.title = title;
this.assignees = assignees;
}
@OneToOne(mappedBy = "taskRefference")
@NotFound(action = NotFoundAction.IGNORE)
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
存储库
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
Employee findById(int id);
}
控制器
@Autowired
EmployeeRepository employeeRepository;
@RequestMapping(value = "/listEmployees", method = RequestMethod.GET)
public List<Employee> listAllEmployees() {
return employeeRepository.findAll();
}
异常:无限递归(StackOverflowError)(通过参考链:com.models.dto.Task [&#34;员工&#34;] - &gt; com.models.dto.Employee [ &#34; taskRefference&#34;] - &gt; com.models.dto.Task [&#34;员工&#34;] - &gt; com.models.dto.Employee [&#34; taskRefference&#34;](它继续100行相同的循环)