在 Spring/Hibernate 中删除具有 FK 约束的对象
对我来说,这似乎是一个简单的场景,但我一生都无法在网上或印刷品中找到解决方案。我有几个像这样的对象(精简):
@Entity
public class Group extends BaseObject implements Identifiable<Long> {
private Long id;
private String name;
private Set<HiringManager> managers = new HashSet<HiringManager>();
private List<JobOpening> jobs;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="group_hiringManager",
joinColumns=@JoinColumn(name="group_id"),
inverseJoinColumns=@JoinColumn(name="hiringManager_id")
)
public Set<HiringManager> getManagers() {
return managers;
}
@OneToMany(mappedBy="group", fetch=FetchType.EAGER)
public List<JobOpening> getJobs() {
return jobs;
}
}
@Entity
public class JobOpening extends BaseObject implements Identifiable<Long> {
private Long id;
private String name;
private Group group;
@ManyToOne
@JoinColumn(name="group_id", updatable=false, nullable=true)
public Group getGroup() {
return group;
}
}
@Entity
public class HiringManager extends User {
@ManyToMany(mappedBy="managers", fetch=FetchType.EAGER)
public Set<Group> getGroups() {
return groups;
}
}
假设我想删除一个 Group 对象。现在JobOpening表和group_hiringManager表中存在对其的依赖,导致删除功能失败。我不想级联删除,因为经理有其他组,并且职位空缺可以是无组的。我尝试重写 GroupManager 的 remove() 函数来删除依赖项,但似乎无论我做什么,它们都会持续存在,并且删除失败!
删除这个对象的正确方法是什么?
This seems like such a simple scenario to me, yet I cannot for the life of my find a solution online or in print. I have several objects like so (trimmed down):
@Entity
public class Group extends BaseObject implements Identifiable<Long> {
private Long id;
private String name;
private Set<HiringManager> managers = new HashSet<HiringManager>();
private List<JobOpening> jobs;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="group_hiringManager",
joinColumns=@JoinColumn(name="group_id"),
inverseJoinColumns=@JoinColumn(name="hiringManager_id")
)
public Set<HiringManager> getManagers() {
return managers;
}
@OneToMany(mappedBy="group", fetch=FetchType.EAGER)
public List<JobOpening> getJobs() {
return jobs;
}
}
@Entity
public class JobOpening extends BaseObject implements Identifiable<Long> {
private Long id;
private String name;
private Group group;
@ManyToOne
@JoinColumn(name="group_id", updatable=false, nullable=true)
public Group getGroup() {
return group;
}
}
@Entity
public class HiringManager extends User {
@ManyToMany(mappedBy="managers", fetch=FetchType.EAGER)
public Set<Group> getGroups() {
return groups;
}
}
Say I want to delete a Group object. Now there are dependencies on it in the JobOpening table and in the group_hiringManager table, which cause the delete function to fail. I don't want to cascade the delete, because the managers have other groups, and the jobopenings can be groupless. I have tried overriding the remove() function of my GroupManager to remove the dependencies, but it seems like no matter what I do they persist, and the delete fails!
What is the right way to remove this object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在删除组之前,迭代组职位空缺列表并将组字段设置为 null。或者编写一个更新查询,将职位空缺表的组字段清空。类似于,
带有查询,类似于,
然后删除组。
Before deleting group, iterate over groups job opening list and set null to group field. Or write an update query which nulls job openings table's group field. Something like,
With query, something like,
Then remove group.