【问题标题】:Spring JPA repository many to many mapping creating duplicate while adding recordsSpring JPA 存储库多对多映射在添加记录时创建重复项
【发布时间】:2021-07-31 08:28:54
【问题描述】:

我有两个通过多对多关系关联的实体。现在我创建了一条新记录,我需要更新关系,但是在这样做的同时,我在一个子表中获得了每个父记录的重复条目。下面是实体代码

@Entity
@Table(name = "Employee")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String title;
    @ManyToMany(fetch = FetchType.EAGER,
            cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
            },
            mappedBy = "employees")
    private Set<Department> departments = new HashSet<>();

    public Employee() {
    }

    
    public Employee(String title) {
        this.title= title;
    }

    public Integer getId() {
        return id;
    }


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public Set<Department> getDepartments() {
        return departments;
    }


    public void setDepartments(Set<Department> departments) {
        this.departments = departments;
    }

    @Override
    public String toString() {
        return "Colmn [id=" + id + ", title=" + title + "]";
    }

    
}


@Entity
@Table(name = "Department")
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String company;
    
    @ManyToMany(fetch = FetchType.EAGER,
            cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
            })
    @Fetch(FetchMode.JOIN)
    @JoinTable(name = "dept_emp",
            joinColumns = { @JoinColumn(name = "dept_id") },
            inverseJoinColumns = { @JoinColumn(name = "emp_id") })
    private Set<Employee> employees = new HashSet<>();
    
    

    public Department() {
    }

    public Department(String company) {
        this.company = company;     
    }

    
    public long getId() {
        return id;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(long company) {
        this.company = company;
    }

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

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


    @Override
    public String toString() {
        return "Department [id=" + id + ", company=" + company + ", employees=" + employees + "]";
    }
}

我正在使用 spring curd 存储库

public interface DepartmentRepository extends CrudRepository<Department, Long> {
}


public interface EmployeeRepository extends CrudRepository<Employee, Long> {
}

现在我在数据库中有现有记录 我想在系统中添加一个属于所有部门的新员工,所以我在下面做了

/**Search all the existing departments*/
    List<Department> depts = (List<Department>) departmentRepository.findAll();
    
    /**Create the new employee record*/
    Employee employee = new Employee();
    employee.setTitle("Adam");
    
    /**Iterate and create the relatonship*/
    for (Department d : depts) {
        d.getEmployees().add(employee);
        employee.getDepartments().add(d);
        departmentRepository.save(d);
    }

问题在于我在员工表中插入了一条记录的每个部门,即如果我有 5 个部门,那么我在员工表中插入了 5 次员工记录,即“Adam”。部门表没有任何问题。

如何处理

【问题讨论】:

  • 在最后一行中,您正在保存一个名为“r”的对象......这个“r”来自哪里?
  • Srry 拼写错误,已更正,但问题仍然存在

标签: spring hibernate spring-data-jpa many-to-many


【解决方案1】:

自然会发生这种情况,您需要先保留新员工,然后再添加并保存部门。否则 jpa 对该员工一无所知,因此无法对其进行管理。

所以请调用employee = employeeRepository.save(employee),然后将其添加到您想要的所有部门。

确保您添加的是从保存操作返回的实体 - 保存方法将返回持久化的实体

【讨论】:

    猜你喜欢
    • 2018-03-30
    • 1970-01-01
    • 2018-02-26
    • 2018-01-27
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    相关资源
    最近更新 更多