0
你好 我有以下兩個實體休眠:問題與OnDelete註釋
@Entity
public class DocumentCollection {
@Id
@GeneratedValue
private Long id;
@OneToMany(targetEntity=Document.class,mappedBy="documentCollection",cascade=javax.persistence.CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Document> documents;
...
}
和:
@Entity
public class Document {
@Id
@GeneratedValue
private Long id;
@ManyToOne
private DocumentCollection documentCollection;
...
}
當DocumentCollection被刪除,所有參考文件也應該被刪除。 但我發現了這個錯誤:
Cannot delete or update a parent row: a foreign key constraint fails (`tms_db`.`document`, CONSTRAINT `FK3737353BEB85533C` FOREIGN KEY (`documentCollection_id`) REFERENCES `documentcollection` (`id`))
我也試圖把@OnDelete註釋文檔類,但沒有奏效。 那麼我錯過了什麼?爲了您的信息,我使用Hibernate 3.6.0.Final。
更新:我做了一個mysql模式轉儲,並注意到文檔表模式中沒有ON DELETE CASCADE語句。 Hibernate只會生成外鍵約束,這會導致上述錯誤。有沒有人知道爲什麼hibernate不會生成「ON DELETE CASCADE」語句?
希望,有人能幫助我 THX
thx您的回覆,我在哪裏可以找到由休眠生成的模式? – krebbl 2011-05-18 14:36:43
@ krebbl看你的外鍵。 – okwap 2016-06-21 08:21:19