Hibernate的增删查改
删除:
1、org.springframework.dao.DataIntegrityViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`life`.`tb_ad`, CONSTRAINT `FK690A3947B8D3F97` FOREIGN KEY (`coupon_id`) REFERENCES `tb_coupon` (`id`));
在删除对象coupon时,因为Coupon和CouponAd存在一对一的关系,coupon的id作为couponad的外键,所以会出错。
解决方法:级联删除配置
@OneToOne(cascade={CascadeType.PERSIST,CascadeType.REFRESH,CascadeType.REMOVE})
@JoinColumn(name="coupon_id",unique=true)
private Coupon coupon;
Done