How to delete Child or Parent objects from Relationship?
Asked Answered
C

3

5

I did a small application with more relationships. Now I want to delete details of my table how can I delete I don't get any Idea to delete.

Relationships are like below:

PanCard-->Employee (Ono To One)

Employee-->ProjectManger (bi-directional many-to-one association to Employee)

Projects -->ProjectManager(bi-directional many-to-one association to Projects)

Now I want delete the data of one by one table data

Below is my POJO classes Code:

PanCard.java

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="id")
  private int id;
  @Column(name="pName")
  private String pName;
  @Column(name="pNumber")
  private int pNumber;    
  @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
  @JoinColumn(name="EId")
  private Employee employee;

Employee.java

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "empFirstName")
private String empFirstName;
@Column(name = "empLastName")
private String empLastName;
@Column(name = "empDepartment")
private String empDepartment;   
@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="pmId")      
private ProjectManager projectManager;

ProjectManager.java

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

private String department;

private String managerFirstName;

private String managerLastName;

//bi-directional many-to-one association to Myemployee

@OneToMany(mappedBy="projectManager",cascade = CascadeType.ALL)
private List<Employee> employee;

@OneToMany(mappedBy="projectManager",cascade = CascadeType.ALL)
private List<Projects> projects;

Projects.java

 @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="id")
  private int id;
  @Column(name="projectName")
  private String projectName;
  @Column(name="projectDesc")
  private String projectDesc;     
  @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
  @JoinColumn(name="pmId")    
  private ProjectManager projectManager;

Now I want delete the tables data: From which table I should start deletion.

  • If I want to delete Pancard I should delete ProjectManager because Employee have FK.
  • If I want to delete ProjectManager it should delete the Employee and Projects but Employee have relationship with PanCard So it's not deleting.
  • If I want to delete Projects it should delete ProjectManager but ProjectManger have relation ship with Employee So it's not deleting.

So from where I have to start deletion and how can I delete the I don't know.

Caulfield answered 5/2, 2014 at 6:18 Comment(2)
Why are you creating seperate threads for the same question? Link to your previous question, https://mcmap.net/q/1482628/-many-to-one-relationship-while-removing-child-object-it-39-s-throwing-exception/2968614Ruthi
@Ruthi I am sorry I don't know how linkCaulfield
B
5

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.

Blatman answered 5/2, 2014 at 7:4 Comment(2)
Thank you sir. So in Which POJO classesI have to remove the CascadtType.ALLCaulfield
You're welcome. I would restrict the cascade from Employee to ProjectManager and from Project to ProjectManager. You dont have to completely remove all cascading, but you should not need the REMOVE cascade.Blatman
P
0

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.

Pelayo answered 5/2, 2014 at 6:25 Comment(1)
Thank you when i am deleting ProjectManager it's throwing exception like. Cannot delete or update a parent row: a foreign key constraint fails (prabha.pancard, CONSTRAINT FK_pancard_EId FOREIGN KEY (EId) REFERENCES employee (id)) DELETE FROM employee WHERE (id = ?)Caulfield
J
0

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

Jevon answered 5/2, 2014 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.