Hibernate删除时的级联问题

来源:互联网 发布:可可网络验证通杀 编辑:程序博客网 时间:2024/06/08 11:05

在用HQL语句进行级联删除时,总是报以下错:

[SpringTransaction] 2016-09-02 09:24:45   Cannot delete or update a parent row: a foreign key constraint fails (`ssh`.`employee`, CONSTRAINT `FK_fcr4kbsnt6c84hvcd3jg3usue` FOREIGN KEY (`dno`) REFERENCES `department` (`did`)) [SpringTransaction] 2016-09-02 09:24:45   Exception occurred during processing request: could not execute statement org.hibernate.exception.ConstraintViolationException: could not execute statement    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)

网上说是外键约束导致的不能删除,但我在两端都设置了cascade属性:

<set name="employees" cascade="all" inverse="false">            <key column="dno"></key>            <one-to-many class="entity.Employee"/>        </set>
<many-to-one cascade="all" name="department" class="entity.Department" column="dno" lazy="false"></many-to-one>

在Navicat里面查看了一下表结构,发现:
这里写图片描述
可我明明都设置了cascade=”all”属性,但数据库里面就是没有保存到该属性,重新删掉表再用Hibernate创建,仍然是这样。

最后只好在Navicat里面强制吧RESTRICT改为CASCASDE。


网上有关于级联删除的文章:
表删除时 Cannot delete or update a parent row: a foreign key constraint fails 异常处理

自己还是浮躁了,真应该少lu点了。。。

0 0