问题描述
我做了一个有更多关系的小应用程序。现在我想删除我的表格的详细信息我该如何删除我没有任何想法删除。
关系如下所示:
现在我想删除一个一个表数据的数据
下面是我的POJO类代码:
p>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)
私人员工雇员;
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;
私人String部门;
private String managerFirstName;
private String managerLastName;
//双向多对一关联到Myemployee
@OneToMany(mappedBy =projectManager,cascade = CascadeType.ALL)
private List< ;员工>雇员;
@OneToMany(mappedBy =projectManager,cascade = CascadeType.ALL)
private List< 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;
现在我要删除表格数据:从哪个表格开始删除。
- 如果我想删除 Pancard 我应该删除 ProjectManager $ c因为 Employee 有FK。
- 如果我想删除 ProjectManager 它应该删除员工和项目但是员工有与 PanCard的关系因此它不会被删除。
- 如果我想删除项目它应删除 ProjectManager 但 ProjectManger 与员工所以它不是删除。
因此,我必须从哪里开始删除,如何删除我不知道的
您可以从任何您喜欢的表中开始删除。但是,如果您想为此使用JPA,则必须防止约束违规。您可以通过从拥有方删除
- 或
- 来取消设置外键然后从反面删除。
澄清关系映射:
- PanCard 是与 Employee 关系的拥有方。
- Employee 是与 ProjectManager 的关系的拥有者。 li>
- Project 是与 ProjectManager 关系的拥有者。
一般性建议 - 不要在多方面使用级联。结果大多不是你想要的 - 违反约束条件。如果删除 Employee ,则删除将级联到 ProjectManager 。由于经理可以有多个员工,因此 Employee 表中的外键约束将被违反。所以我会从 Employee 到 ProjectManager 中删除层叠。
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:
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.
这篇关于如何从关系中删除子对象或父对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!