How to delete Child or Parent objects from Relationship?

强颜欢笑 提交于 2019-12-01 00:03:28

You can start deleting from any table you like. However, if you want to use JPA for that, you will have to prevent constraint violations. You can do that by either

  • deleting from the owning side, or
  • unsetting the foreign keys on the owning entities and then deleting from the inverse side.

To clarify the relationship mappings:

  • PanCard is the owning side of the relationship with Employee.
  • Employee is the owning side of the relationship with ProjectManager.
  • Project is the owning side of the relationship with the ProjectManager.

A general advice - do not use cascades from the many side. The outcome is mostly not what you would want - a constraint violation. If you delete an Employee, the removal is cascaded to the ProjectManager. Since a manager can have multiple employees, the foreign key constraint in the Employee table would be violated. So I would remove the cascade from Employee to ProjectManager.

In short:

  1. Get an open entitymanager

  2. Begin a transaction

  3. Remove the necessary references (i.e perform the operations you want to do, but ensure that afterwards all constraints are satisfied)

  4. Commit the transaction

  5. Close the entitymanager

siddharth gupta

Why should you delete the project manager while deleting the employee?
It's not one to one relationship, there are other employees and other projects too under the same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!