1
Class Employee{
//one to one
Department dep;
}

Class Department{
String name

}

My question is how do I handle a scenario where I have to delete a department but not the employee row Do I need to manually delete a reference of a department from an employee and then delete the dep or can I use a cascade feature but in that case do not want to delete the parent.

  • 2
    I'm pretty sure that you'd want Many-To-One from Employee to Department. If you'd also add One-To-Many from Department to Employee, you could use the @PreRemove feature as described [here](https://stackoverflow.com/a/47435269/561543) – Don Ho Jul 21 '21 at 10:58
  • Thanks for your answer.I thought of using one to many but wanted to explore one to one too – Dhirendra Singh Jul 21 '21 at 15:10

1 Answers1

0

Yes, you should be able to use a cascading delete in the database you are using. The cascading delete generally allows at least two options:

  • on delete cascade which deletes the rows in the referencing table (what you do not want).
  • on delete set null which sets the reference to NULL.

Your particular database may offer other options as well. You should check the documentation.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786