gpt4 book ai didi

java - 在 @OneToMany 中的 hibernate 中删除 set null

转载 作者:行者123 更新时间:2023-12-03 12:52:37 27 4
gpt4 key购买 nike

我有一个部门实体,其关系如下:

  • 多部门可以在一个上级部门 :
    @ManyToOne
    @JoinColumn(name = "ik_parent_department_id")
    private Department parentDepartment;
  • 一个上级部门可以有多部门 :
    @OneToMany(mappedBy = "parentDepartment")
    private Set<Department> children = new HashSet<Department>(0);

  • 我想实现下一个:当我 删除 部门 ,然后是 ik_parent_department_id 所有参数 child 部门设置为 .任何想法如何做到这一点?

    最佳答案

    您必须设置 child 的 ik_parent_department_id显式为空。

    Department parentDepartment = (Department) session.load(Department.class, id);
    session.delete(parentDepartment);
    for (Department child : parentDepartment.getChildren()){
    child.setParentDepartment(null);
    }
    session.flush();

    通过级联,您只能设法删除子项 Departments .

    关于java - 在 @OneToMany 中的 hibernate 中删除 set null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8243400/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com