有两张表,结构如下:
- t_item: t_bid:
- id int id int
- name varchar name varchar
- item_id int
- 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`))
(1) 增加外键约束时声明级联删除,即:
- alter table t_bid add constraint fk_id foreign key(id) references
- key(id) on delete cascade;
触发器代码(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中相对容易解决,只需设置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=?
- <many-to-one name="item" column="item_id" class="po.Item" not-null="true"/>
- <set name="bids" cascade="all">
- <key column="item_id" not-null="true"/>
- <one-to-many class="po.Bid"/>
- </set>
如果我们指定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
- •••
- 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".
- 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=?