我有父母Filter
實體和一個方向List<Ad>ads
與子女@OneToMany
的關係。我嘗試刪除廣告,其中更多的則是隻有一個星期的使用Hibernate hql
查詢,但得到:MySQLIntegrityConstraintViolationException用於Hibernate hsql刪除。無法刪除或更新父行
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:不能刪除或更新父行,外鍵約束失敗( 。
gecars
filters_ads
,約束FK_gcri6h0918u8o2ybd6yfquk79
外鍵(ads_id
)參考文獻ads
(id
))
用戶:
@Entity
@Table(name="users")
public class User {
...
@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="user")
@OrderBy("id")
private Set<Filter> filters = new HashSet<Filter>(0);
...
過濾器:
@Entity
@Table(name="filters")
public class Filter {
...
// I also tried use @OneToMany(CascadeType.REMOVE, orphanremoval=true)
// or @OneToMany(cascade=CascadeType.ALL,orphanRemoval=true)
@OneToMany
private Set<Ad> ads;
...
廣告:
@Entity
@Table(name="ads")
public class Ad {
...
private Date insertTime = new Date();
...
DAO刪除方法:
public void deleteOldAds(Date date){
String hql = "delete from Ad where insertTime < :date";
session.createQuery(hql).setParameter("date", date).executeUpdate();
}
嘗試級聯都在那裏:@ OneToMany(cascade = CascadeType.ALL) –
我試過了。不工作,得到相同的例外。 –
你可以看看這裏http://stackoverflow.com/questions/32146911/hibernate-throws-cannot-delete-or-update-a-parent-row-a-foreign-key-constraint –