我的要求是当我删除父,子不应该被删除,而不是删除子,其中一个子列department_ID
是外键必须更新一些值,说一个部门(父表)包含许多员工和员工(子表)表Department_ID
作为外键列,面临的问题是我当我删除父表没有反映在孩子因为我有让updatable=false
如果我将使它真正删除父,子列设置为空或如果我更新父母也孩子Department_Id
列设置为null,违反要求所以我使它错误的。我不希望孩子当父母做更新任何更改都应该反映在删除操作上,仅反映在一个列上。请帮帮我,我该怎么办?由于
这是我的父表部门代码
@Id
@Column(name = "DEPARTMENT_ID")
private String departmentId;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "DEPARTMENT_ID",insertable=false,updatable=false)
Collection<Employee> employeeList;
This is my child Table employee
@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;
让子端保持一对多的关系,所以需要给@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