级联仅对从 父级 传播到 子级的 实体状态转换有意义。在您的情况下,“父母”实际上是该协会(具有FK)的孩子。
尝试使用此映射:
@Entitypublic class Parent { ... @oneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "parent") private Child child; ...}@Entitypublic class Child { @oneToOne @JoinColumn(name = "parent_id") private Parent parent; ... @Lob private byte[] data; ...}并且要级联删除孤儿,您现在需要:
Parent parent = ...;parent.getChild().setParent(null);parent.setChild(null);
甚至更好的是,在
Parent实体类中使用addChild / removeChild方法:
public void addChild(Child child) { children.add(child); child.setParent(this);}public void removeChild(Child child) { children.remove(child); child.setParent(null);}有关更多详细信息,请查看本文。



