简体   繁体   中英

ManyToOne and OneToMany is giving stackoverflow error

Project is based on JPA persistance with two Entities (Deaprtment and Employee) Department(OneToMany) and Employee(ManyToOne) Whenever I send a request via API there's a StackOverFlow error. So far I back Traced the main cause which is the stack is full is indefinite recursion. Could someone explain why this happened ususally it shouldn't have confused by bidirectioanl relationship of entities.

在此处输入图像描述

package com.springjpacrud01.model;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "department")
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "name")
    private String name;

    @OneToMany(mappedBy = "department", fetch = FetchType.LAZY)
    List<Employee> employees;

    public Department() {    }

    public Department(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }


}


import com.fasterxml.jackson.annotation.JsonBackReference;

import javax.persistence.*;

@Entity
@Table(name = "employees")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "name")
    private String name;
    @Column(name = "position")
    private String position;

    @ManyToOne
    @JoinColumn(name = "department_id")
    private Department department;

    public Employee(Long id, String name, String position) {
        this.id = id;
        this.name = name;
        this.position = position;
    }

    public Employee() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }
}

After I just deleted the getters/setters from the Department entity it worked but it shouldn't have work like that and I want why I cannot do relation pointing to each other entities? It couldn't form JSON response because of the infinite recursion of pointing to each other I guess. How can I solve it effectively in order to retrieve Employees by department ID, thank you)

If someone needs it I've solved this by understanding the deep root of the cause which was @JoinColumn created and addressed by Hibernate to that empty common column which I deleted manually. And when I was requesting the department_id of the employee via the employee repository Hibernate sort of got stuck in an infinite loop of going to the employee repository and from there to the department repository and in the department repository going to the employee repository. To stop that I've mapped the relation differently by making a configuration of the department @OneToMany(mappedBy = "department", cascade= CascadeType.ALL, orphanRemoval=true) private Set<Employee> employeeHashSet = new HashSet<>(); And

@ManyToOne
@JoinColumn(name = "department_id")
private Department department;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM