[英]Hibernate unidirectional many-to-one and cascading delete
i am newbie in Hibernate and i need unidirectional many-to-one with on delete cascade. 我是Hibernate的新手,我需要在删除级联上单向多对一。
Apparently the only option is, when the parent knows about its children. 显然唯一的选择是,当父母知道其子女时。 But what if I do it manually via SQL?
但是,如果我通过SQL手动执行该操作呢? Is this solution good?
这个解决方案好吗?
alter table CHILDREN
add constraint FK_i4jjy61om foreign key (PARENT)
references PARENT
on delete cascade;
You can use SQL ON DELETE CASCADE
, but you have to be careful to evict all Child entities belonging to the Parent you are about to delete. 您可以使用SQL
ON DELETE CASCADE
,但是您必须小心驱逐属于您要删除的Parent的所有Child实体。
If you already loaded a Parent's Child, when you delete the Parent, the Child will be deleted from the database without Hibernate knowing about the entity state change . 如果您已经加载了Parent's Child,那么当您删除Parent时,将从数据库中删除Child,而Hibernate不知道实体状态更改 。
To address this situation, it's better to evict all associated Child entities, to make sure you don't accidentally re-persist them through some other associations transitive persistence . 为了解决这种情况,最好驱逐所有相关的子实体,以确保不会意外地通过其他关联传递持久性来重新持久化它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.