Have JPA/Hibernate to replicate the “ON DELETE SET NULL” functionality

后端 未结 3 1012
迷失自我
迷失自我 2020-12-01 05:20

I\'ve been able to have JPA/Hibernate to replicate the ON DELETE CASCADE functionality successfully (seems like the default behaviour) but I\'m now trying to re

3条回答
  •  不思量自难忘°
    2020-12-01 05:46

    It doesn't appear to be possible at the moment with jpa/hibernate.

    On delete set null in hibernate in @OneToMany

    JBs solution seems clean though:

    for (Department child : parent.getChildren()) {
        child.setParentDepartment(null);
    }
    session.delete(parent);
    

    You should also be able to put it in a PreRemove:

    @PreRemove
    private void preRemove() {
        for (Student s : studentList) {
            s.setTeacher(null);
        }
    }
    

提交回复
热议问题