问题
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 deleteProjectManager
becauseEmployee
have FK. - If I want to delete
ProjectManager
it should delete theEmployee
andProjects
butEmployee
have relationship withPanCard
So it's not deleting. - If I want to delete
Projects
it should deleteProjectManager
butProjectManger
have relation ship withEmployee
So it's not deleting.
So from where I have to start deletion and how can I delete the I don't know.
回答1:
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 withEmployee
.Employee
is the owning side of the relationship withProjectManager
.Project
is the owning side of the relationship with theProjectManager
.
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
.
回答2:
In short:
Get an open entitymanager
Begin a transaction
Remove the necessary references (i.e perform the operations you want to do, but ensure that afterwards all constraints are satisfied)
Commit the transaction
Close the entitymanager
回答3:
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.
来源:https://stackoverflow.com/questions/21569970/how-to-delete-child-or-parent-objects-from-relationship