سؤال

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.

هل كانت مفيدة؟

المحلول

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

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top