1

my requirement is when i delete the parent ,child should not be deleted but instead of deleting child,one of the child column department_ID which is foreign key must be update with some value , say one department(Parent Table) contains many employee and employee(child Table) table have Department_ID as a foreign key column .The problem is i m facing when i delete the parent table nothing is reflected on child cause i have make updatable=false if i will make it true then on deleting parent ,child column is set null or if i update the parent then also child Department_Id column set to null which is violating the requirement so that's why i make it false .I don't want child to be update when the parent make any changes only changes should reflected on delete operation ,only on one column. Please help me what should i do? thanks

This is my parent table Department code

  @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;
henrycharles
  • 1,019
  • 6
  • 28
  • 66

1 Answers1

0

Let children-side to maintian one-to-many relationship, so you need to add mappedBy attribute to @OneToMany.

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

Maintain relationsip as below:

employee.departmentId = newDepartmentId; //or set it null to detach from the old dept
Henry Leu
  • 2,184
  • 4
  • 22
  • 34