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:
@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;
@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;
@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;
@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.
Pancard
I should delete ProjectManager
because Employee
have FK.ProjectManager
it should delete the Employee
and Projects
but Employee
have relationship with PanCard
So it's not deleting.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
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
.