简体   繁体   English

ManyToOne 和 OneToMany 给出计算器错误

[英]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.项目基于 JPA 与两个实体(部门和员工)部门(OneToMany)和员工(ManyToOne)的持久性每当我通过 API 发送请求时,都会出现 StackOverFlow 错误。 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?在我刚刚从 Department 实体中删除 getter/setter 后,它起作用了,但它不应该那样工作,我想知道为什么我不能做指向彼此实体的关系? It couldn't form JSON response because of the infinite recursion of pointing to each other I guess.它无法形成 JSON 响应,因为我猜是因为指向彼此的无限递归。 How can I solve it effectively in order to retrieve Employees by department ID, thank you)我怎样才能有效地解决这个问题,以便通过部门 ID 检索员工,谢谢)

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.如果有人需要它,我已经通过了解@JoinColumn 创建的深层原因解决了这个问题,并由 Hibernate 解决了我手动删除的空公共列。 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.当我通过员工存储库 Hibernate 请求员工的 department_id 时,我陷入了一个无限循环:转到员工存储库,然后从那里转到部门存储库,然后在部门存储库中转到员工存储库。 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<>();为了阻止这种情况,我通过配置部门@OneToMany(mappedBy = "department", cascade= CascadeType.ALL, orphanRemoval=true) private Set<Employee> employeeHashSet = new HashSet<>(); And

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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