我試圖從表中刪除子項,我的實體擁有2路的關係。我收到了一個異常「無法刪除或更新父行:外鍵約束失敗」。 我需要映射當我刪除與父母的子條目關係自動丟棄。JPA:不能刪除或更新父行
@Entity
@Table
public class RuleModel implements Comparable<RuleModel> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Rule_Id", unique = true, nullable = false)
protected Integer id;
@OneToOne(fetch = FetchType.EAGER)
protected RuleModel parent;
@OneToOne(fetch = FetchType.EAGER)
protected RuleModel child;
}
您需要設置CASCAD降:http://stackoverflow.com/questions/ 306144/jpa-cascadetype-all-does-not-delete-orphans – Zorglube
但我不想刪除我的父項。 – Hammad
對於你的情況,也許'父'和'孩子'都引用同一個對象。請檢查您的代碼。 –