无法删除休眠中的外键约束

Unable to remove foreign key constraint in hibernate

我正试图从我的项目中删除一个对象。 它依赖于许多其他 类。我知道我需要将该对象从所有其他依赖项中释放出来。我试图删除它的所有依赖项,但我仍然收到外键约束错误

这是我的 类

class LabAssistant{
    // variables
    @OneToMany(cascadeType.All,
               orphanRemoval=true,
               mappedBy="labAssistant")
    private List<LabRecord> labRecords;
    
    //Getters and setters
}


class LabRecord {
    //variables
    @OneToMany(cascade = CascadeType.ALL, mappedBy="labRecord")
    private List<Test> tests;

    @ManyToOne
    @JoinColumn(name = "lab_id")
    private LabAssistant labAssistant;

    //Getters and Setters
}

class Test{
    @Lob
    private byte[] testReport;

    @ManyToOne
    @JoinColumn(name = "lab_record_id")
    private LabRecord labRecord;

    @ManyToOne
    @JoinColumn(name = "test_info_id")
    private TestInfo testInfo;

    //Getters and setters
}

boolean deleteAssistant(int id){
  LabAssistant labAssistant = session.load(LabAssistant.class, id);
  for(LabRecord l : labAssistant.getLabRecords()){
    for(Test t: l.getTests()){
      t.setTestInfo(null);
    }
    l.getTests().clear();
  }
}
labAssistant.getLabRecords().clear();
session.delete(labAssistant);
return true;
}

我仍然收到外键约束错误

'db'.'tests', Constraint 'fkey' Foreign Key ('lab_record_id') References 'lab_records'('id')

感谢任何帮助!!

当建立LabAssistant和LabRecord之间的关系为CascadeType.ALL和orphanRemoval = true时,分配给LabAssistant的LabRecord也将被删除,大概你有与这些LabRecord之一相关的测试条目,阅读这个:

https://www.objectdb.com/java/jpa/persistence/delete

以某种方式重新创建数据库解决了问题。