有两张表,结构如下:

t_item:                          t_bid:
id        int                     id        int
name    varchar                   name      varchar  item_id   int    

其中表t_item的主键id是表t_bid的item_id字段的外键。那么在这种情况下,如果删除表t_item中的记录,并且该记录中的id主键被t_bid中的item_id字段所引用,就会抛出如下异常:

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`test/t_bid`, CONSTRAINT `fk_id` FOREIGN KEY (`id`) REFERENCES `t_item  ` (`id`))   

解决方法:级联删除,即在删除t_item表中的记录时同时删除t_bid表中的相关记录 。

(1) 增加外键约束时声明级联删除,即:

alter table t_bid add constraint fk_id foreign key(id) references
key(id) on delete cascade;

(2)  使用触发器:在删除t_item表中记录之前先删除与之相关的t_bid表中的记录。 (这方法较好)
     触发器代码(MySQL):

delimiter //
create trigger tri_delete before delete on t_item
for each row
begin  delete from t_bid where id = old.id;
end // 

Hibernate中的解决方案: 
这个问题在Hibernate中相对容易解决,只需设置cascade = “delete”即可。此时观察发出的sql语句:

Hibernate: select item0_.id as id0_0_, item0_.name as name0_0_ from t_item item0_ where item0_.id=?  Hibernate: select bids0_.item_id as item3_1_, bids0_.id as id1_, bids0_.id as id1_0_, bids0_.price as price1_0_, bids0_.item_id as item3_1_0_ from t_bid bids0_ where bids0_.item_id=?  Hibernate: update t_bid set item_id=null where item_id=?  Hibernate: delete from t_bid where id=?  Hibernate: delete from t_bid where id=?  Hibernate: delete from t_item where id=?  

发现在删除t_bid表中记录之前会先将它的item_id字段值设置为null,但如果我们在映射文件中设置item_id字段不能为null,即设置Bid.hbm.xml文件为:

<many-to-one name="item" column="item_id" class="po.Item" not-null="true"/>  

注意不能在Item.hbm.xml文件中进行如下设置(即在key元素中指定not-null="true"):

<set name="bids" cascade="all">  <key column="item_id" not-null="true"/>   <one-to-many class="po.Bid"/>
</set>   

这样会抛出"Repeated column in mapping for entity"异常
如果我们指定item_id字段值不能为null,那么在删除时会抛出如下异常:

org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update···
Caused by: java.sql.BatchUpdateException: Data truncation: Column set to default value; NULL supplied to NOT NULL column 'item_id' at row 1
•••  

此时的解决方法是设置inverse="true",这在Hibernate文档中有相应的描述:

Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, Hibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true".   

观察此时发出的sql语句:

Hibernate: select item0_.id as id1_0_, item0_.name as name1_0_ from t_item item0_ where item0_.id=?  Hibernate: select bids0_.item_id as item3_1_, bids0_.id as id1_, bids0_.id as id0_0_, bids0_.amount as amount0_0_, bids0_.item_id as item3_0_0_ from t_bid bids0_ where bids0_.item_id=?  Hibernate: delete from t_bid where id=?  Hibernate: delete from t_bid where id=?  Hibernate: delete from t_item where id=?  

表删除时 Cannot delete or update a parent row: a foreign key constraint fails 异常处理相关推荐

  1. MySQL删除表:Cannot delete or update a parent row: a foreign key constraint fails。

    MySQL库中有俩表,table1和table2,相互关联,在删除表的时候出错: Cannot delete or update a parent row: a foreign key constra ...

  2. 4 Flask开发进阶:记下一次级联删除的报错:Cannot delete or update a parent row: a foreign key constraint fails

    中间表 class Permission(object):LOGIN = 0x01EDITOR = 0x02OPERATOR = 0x04ADMINISTER = 0xffPERMISSION_MAP ...

  3. mysql删除有外链索引数据,Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法

    mysql删除有外链索引数据,Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法 参考文章: (1 ...

  4. Mysql报错 Cannot delete or update a parent row: a foreign key constraint fails

    mysql数据库中使用delete语句删除表中的一条信息时,报错:1451 - Cannot delete or update a parent row: a foreign key constrai ...

  5. 解决Cannot delete or update a parent row: a foreign key constraint fails的mysql报错

    1 问题 删除有外键关联的数据或者表的时候,mysql出现报错: Cannot delete or update a parent row: a foreign key constraint fail ...

  6. MySQL遇到错误:1217 - Cannot delete or update a parent row: a foreign key constraint fails

    遇到问题 今天在准备"删库跑路"的时候,有的数据表再删除的时候报了以下错误: 1217 - Cannot delete or update a parent row: a fore ...

  7. ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`meiduo`.`

    在MySQL中删除一张表或一条数据的时候,出现 这是因为MySQL中设置了foreign key关联,造成无法更新或删除数据.可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况. 我们 ...

  8. 删除有外键关联的表报错,Cannot delete or update a parent row: a foreign key constraint fails

    解决办法: SET foreign_key_checks = 0; //先设置取消外键检查 drop table portal_user; SET foreign_key_checks = 1; // ...

  9. 删除订单时报错Cannot delete or update a parent row: a foreign key constraint fails (`jxc`.`t_purchase_list_

    删除数据的id与其他表的id有外键关联,所以关联的数据也要删除 注:先删主键,再删外键.

最新文章

  1. 人脸识别剩下的难题:从遮挡,年龄,姿态,妆造到亲属关系,人脸攻击
  2. 单用户多角色权限的MSSQL实现
  3. Centos7 yum安装Python3.6环境,超简单
  4. Ubuntu中设置环境变量PATH
  5. mysq和mysqli关系
  6. 阿里测试环境运维及研发效率提升之道
  7. vsftpd FTP Server ‘ls.c‘ 远程拒绝服务漏洞(CVE-2011-0762)漏洞修复方案
  8. excel判断单元格包含指定内容的函数用=IF(COUNTIF(A1,*内容*),0,1)
  9. 软件测试——测试基础
  10. mysql跨服务器查询
  11. 手机客户端(EZview/智U)添加设备时,为什么会提示“该设备已被其他账号添加”?
  12. 订单信息无法识别,建议联系卖家。 错误码:INVALID_PARAMETER
  13. matlab去除摩尔纹,索尼ILCE-7RM2测评——理论画质篇
  14. cv2 在图片上画线
  15. TVS和一般的稳压二极管有什么区别? (转)
  16. 每天学一点设计模式-工厂方法模式
  17. 业务员与客户沟通的礼仪
  18. 新手C语言开发详细教程
  19. 华为2017年实习招聘上机题
  20. 《Uncertainty-aware Joint Salient Object and Camouflaged Object Detection》—CVPR2021—SOD+COD

热门文章

  1. PP实施经验分享(17)——S4 PP与ME标准接口报工函数“CO_MES_PRODORDCONF_CREATE_TT”
  2. 016-状态自尊|管理他人的切入点
  3. cad卸载工具_CAD安装失败都是红?
  4. error:尝试引用已删除的函数或已显式删除函数
  5. 某公司对新员工的公开信
  6. 使用docker安装mysql5.7
  7. 同城小程序 30.0 完整版源码(含全部插件)
  8. 致谢-10224051-RAL
  9. tensor如何实现转置_转置()TensorFlow中的函数
  10. cadence——基本操作6