老师,在验证商品添加的service中报错,dao层验证都是OK的呢

来源:8-3 商品添加之Service层的实现下

慕无忌3212331

2018-09-05

com.myself.o2o.exceptions.ProductOperationException: 创建商品失败org.springframework.dao.DataIntegrityViolationException: 

### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`o2o`.`tb_product`, CONSTRAINT `fk_product_procate` FOREIGN KEY (`product_category_id`) REFERENCES `tb_product_category` (`product_category_id`))

### The error may involve com.myself.o2o.dao.ProductDao.insertProduct-Inline

### The error occurred while setting parameters

### SQL: INSERT INTO   tb_product(product_name,product_desc,img_addr,   normal_price,promotion_price,priority,create_time,   last_edit_time,enable_status,product_category_id,   shop_id)   VALUES   (?,?,?,   ?,?,?,?,   ?,?,?,   ?)

### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`o2o`.`tb_product`, CONSTRAINT `fk_product_procate` FOREIGN KEY (`product_category_id`) REFERENCES `tb_product_category` (`product_category_id`))

; ]; Cannot add or update a child row: a foreign key constraint fails (`o2o`.`tb_product`, CONSTRAINT `fk_product_procate` FOREIGN KEY (`product_category_id`) REFERENCES `tb_product_category` (`product_category_id`)); nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`o2o`.`tb_product`, CONSTRAINT `fk_product_procate` FOREIGN KEY (`product_category_id`) REFERENCES `tb_product_category` (`product_category_id`))


写回答

1回答

翔仔

2018-09-06

同学好,错误比较明显Cannot add or update a child row: a foreign key constraint fails (`o2o`.`tb_product`, CONSTRAINT `fk_product_procate` FOREIGN KEY (`product_category_id`) REFERENCES `tb_product_category` (`product_category_id`))

违反了外键约束,添加商品的时候,添加了一个不存在的商品类别id,即该商品类别id不在tb_product_category里面存在,请调试下原因

1
0

Java双版本(SSM到SpringBoot)校园商铺全栈开发

SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需

5113 学习 · 8144 问题

查看课程