Cascading only makes sense for entity state transitions that propagate from a Parent to a Child. In your case, the Parent was actually the child of this association (having the FK).
Try with this mapping instead:
@Entity
public class Parent {
...
@OneToOne(
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true,
mappedBy = "parent"
)
private Child child;
...
}
@Entity
public class Child {
@OneToOne
@JoinColumn(name = "parent_id")
private Parent parent;
...
@Lob
private byte[] data;
...
}
And to cascade the orphan removal, you now need to:
Parent parent = ...;
parent.getChild().setParent(null);
parent.setChild(null);
Or even better, confgiure the setChild
method in the Parent
entity class to set both associations:
public void setChild(Child child) {
if (child == null) {
if (this.child != null) {
this.child.setParent(null);
}
}
else {
child.setParent(this);
}
this.child = child;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…