在 Spring/Hibernate 中删除具有 FK 约束的对象

发布于 2024-10-09 01:31:18 字数 1466 浏览 0 评论 0原文

对我来说,这似乎是一个简单的场景,但我一生都无法在网上或印刷品中找到解决方案。我有几个像这样的对象(精简):

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤君无依 2024-10-16 01:31:18

在删除组之前,迭代组职位空缺列表并将组字段设置为 null。或者编写一个更新查询,将职位空缺表的组字段清空。类似于,

Group group = entityManager.find(Group.class, groupId);

for (JobOpening aJob : group.getJobs()) {

     aJob.setGroup(null);
     entityManager.merge(aJob);
     entityManager.flush();
}

//Then delete group

entityManager.remove(group);

带有查询,类似于,

entityManager.createQuery("update JobOpening set group = null where group.id = :groupId")
             .setParameter(groupId)
             .executeUpdate();

然后删除组。

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,

Group group = entityManager.find(Group.class, groupId);

for (JobOpening aJob : group.getJobs()) {

     aJob.setGroup(null);
     entityManager.merge(aJob);
     entityManager.flush();
}

//Then delete group

entityManager.remove(group);

With query, something like,

entityManager.createQuery("update JobOpening set group = null where group.id = :groupId")
             .setParameter(groupId)
             .executeUpdate();

Then remove group.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文