表删除时 Cannot delete or update a parent row: a foreign key constraint fails 异常处理
有两张表,结构如下:
- 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=?
没有发出update语句。
表删除时 Cannot delete or update a parent row: a foreign key constraint fails 异常处理的更多相关文章
- 【转】表删除时 Cannot delete or update a parent row: a foreign key constraint fails 异常处理
转载地址:http://lijiejava.iteye.com/blog/790478 有两张表,结构如下: t_item: t_bid: id ...
- Mysql - 删除表时出现: Cannot delete or update a parent row: a foreign key constraint fails
现象 MySQL在删除一张表时出现 ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint ...
- Hibernate级联删除时:Cannot delete or update a parent row: a foreign key constraint fails异常
在删除主表数据时,报了一个异常 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 问题的解决办法
mysql删除有外链索引数据Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法查询:DELETE ...
- 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 这个该如何解决 可以这样解决: S ...
- MySQL:ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
MySQL在删除一张表时出现 ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fa ...
- Mysql之1451 - Cannot delete or update a parent row: a foreign key constraint fails...解决办法记录
今天使用delete语句删除一张表中的一条信息时,提示了这么一个错误:1451 - Cannot delete or update a parent row: a foreign key constr ...
- ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
mysql 删除表时提示有外键 mysql> drop tables auth_group;ERROR 1217 (23000): Cannot delete or update a paren ...
- MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误
前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sa ...
随机推荐
- 解决 bash cd too many arguments 报错
解决 bash: cd: too many arguments 本来想着用git bash进入文件夹,但是文件夹名称中带有空格,例如:my blog,导致出错. 在查找资料后,找到一种并不可行的方案, ...
- python logger日志通用配置文件
阅读须知⚠️ 1.示例代码可直接放在项目py文件中即可使用 2.project_name,logfile_name变量需根据你的项目进行修改 3.日志输出格式format选择(可根据你的需要替换或修改 ...
- 2019-11-26:密码学基础知识,csrf防御
信息安全的基础是数学--->密码算法--->安全协议(ssl VPN)-->应用(证书 PKI)密码学入门密码编码学:研究加解密算法的学科密码分析学:研究破译密码算法的学科 加解密分 ...
- 【集训Day1 测试】奇怪数
奇怪数(odometer) [题目描述] 一个正整数Z是奇怪数,当且仅当满足的条件是:Z的所有数字中,只有一个数字不同于其他数字.例如:33323.110 都是奇怪数,而 9779.5555 都不是奇 ...
- day 27 网路编程 面向对象多继承
知识补充: 字符串转化为字节 string1 = input(“请输入你的名字”) string1.encode('utf-8') 字节转化为字符串 byte1 = b"alex" ...
- selenium常用命令之页面元素定位
WebDriver driver= new ChromeDriver(); <input type="text" id="phone" name=&q ...
- 为什么阿里巴巴Java开发手册中强制要求不要在foreach循环里进行元素的remove和add操作?
在阅读<阿里巴巴Java开发手册>时,发现有一条关于在 foreach 循环里进行元素的 remove/add 操作的规约,具体内容如下: 错误演示 我们首先在 IDEA 中编写一个在 f ...
- 2019-2020-1 20199304《Linux内核原理与分析》第五周作业
第四章 系统调用的三层机制(上) 4.1 用户态.内核态和中断 知识点总结: 与系统调用打交道的方式是通过库函数的方式. 用户态与内核态的区分 内核态:高的执行级别下,代码可以执行特权指令,访问任意的 ...
- WPF最简单的分页控件
背景:最近在写项目的时候需要写一个简单的分页功能,因项目需要,没有改为MVVM模式,只需要在后台实现 1.呈现效果如下: 接下来就来上代码,看看怎么实现的 1.界面代码 <StackPanel ...
- Spring Cloud第六篇 | Hystrix仪表盘监控Hystrix Dashboard
本文是Spring Cloud专栏的第六篇文章,了解前五篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cloud ...