1

我的要求是当我删除父级时,不应该删除子级,而是删除子级,department_ID作为外键的子列之一必须更新一些值,比如一个部门(父表)包含许多员工和员工(子表)表Department_ID作为外键列。当我删除父表时,我面临的问题没有任何反映在子表上,因为updatable=false如果我将使其变为真然后在删除父表时,子列设置为空或者我更新然后父级也将子Department_Id列设置为null,这违反了要求,所以这就是为什么我将其设为false。我不希望在父级进行任何更改时更新子级,只有更改应该反映在删除操作上,仅反映在一列上。请帮帮我我该怎么办?谢谢

这是我的父表部门代码

  @Id
  @Column(name = "DEPARTMENT_ID")
  private String departmentId;

  @OneToMany(fetch = FetchType.EAGER)
  @JoinColumn(name = "DEPARTMENT_ID",insertable=false,updatable=false)
  Collection<Employee> employeeList;

这是我的孩子 Table 员工

    @Id
    @Column(name = "EMPLOYEE_NUMBER")
    private int employeeNumber;
    @Column(name = "FIRST_NAME")
    private String firstName;
    @Column(name = "LAST_NAME")
    private String lastName;

    @Column(name = "DEPARTMENT_ID")
    private String departmentId;
4

1 回答 1

0

让children-side维护一对多的关系,所以你需要给@OneToMany添加mappedBy属性。

@Id
@Column(name = "DEPARTMENT_ID")
private String departmentId;

@OneToMany(fetch = FetchType.EAGER, mappedBy="departmentId")
@JoinColumn(name = "DEPARTMENT_ID",insertable=false,updatable=false)
Collection<Employee> employeeList;

保持如下关系:

employee.departmentId = newDepartmentId; //or set it null to detach from the old dept
于 2013-03-05T10:18:33.627 回答