繁体   English   中英

ManyToOne 和 OneToMany 给出计算器错误

[英]ManyToOne and OneToMany is giving stackoverflow error

项目基于 JPA 与两个实体(部门和员工)部门(OneToMany)和员工(ManyToOne)的持久性每当我通过 API 发送请求时,都会出现 StackOverFlow 错误。 到目前为止,我回溯到堆栈已满的主要原因是无限递归。 有人可以解释为什么这通常发生,它不应该被实体的双向关系所混淆。

在此处输入图像描述

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;
    }
}

在我刚刚从 Department 实体中删除 getter/setter 后,它起作用了,但它不应该那样工作,我想知道为什么我不能做指向彼此实体的关系? 它无法形成 JSON 响应,因为我猜是因为指向彼此的无限递归。 我怎样才能有效地解决这个问题,以便通过部门 ID 检索员工,谢谢)

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

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

暂无
暂无

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

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