我有以下與@OneToOne關係的實體:無法刪除實體是@OneToOne關係的目標
@Entity
public static class EntityChild extends BaseEntity {
//Id from superclass
}
@Entity
public static class EntityParent extends BaseEntity {
//Id from superclass
@OneToOne(cascade = ALL)
private EntityChild child;
//child getter/setter
}
現在下面的測試不及格:
EntityParent parent = new EntityParent();
em.persist(parent);
em.flush();
EntityChild child = new EntityChild();
parent.setChild(child);
em.persist(parent);
em.flush();
em.remove(parent.getChild());
em.flush();
它拋出異常,在最後行,用flush()。例外是javax.persistence.EntityNotFoundException: deleted entity passed to persist: [my.package.EntityChild#<null>]
爲什麼我不能刪除此實體?
它的工作原理,謝謝@axtavt!不過,我想完全理解它。當我移除孩子然後沖洗時,他們是否看到與這個孩子的關係,發現父母有關係,併合並父母?然後很明顯,在父母中有孩子,所以它試圖堅持下去。是對的嗎? – amorfis 2011-01-20 14:52:19