简体   繁体   English

Hibernate Cascade DELETE OneToMany 不起作用。 违反参照完整性约束

[英]Hibernate Cascade DELETE OneToMany does not work. Referential integrity constraint violation

I have a class Webtoon that contains a list of Episode.我有一个包含剧集列表的 Webtoon 类。 It is a one direction relation.这是一种单向关系。

@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@CollectionTable(name= "list_of_episodes")
List<Episode> listOfEpisodes = new ArrayList<>();

In my Unit test, I created a Webtoon object, then added an episode in the list listOfEpisodes.在我的单元测试中,我创建了一个 Webtoon 对象,然后在 listOfEpisodes 列表中添加了一个剧集。 When I try to delete the Episode using the Episode repository当我尝试使用剧集存储库删除剧集时

this.episodeRepo.delete(episode);

I got the error :我收到错误:

violation de contrainte: "FK50GHKTDAXMN68TBU6KAYVUX9S: PUBLIC.LIST_OF_EPISODES FOREIGN KEY(LIST_OF_EPISODES_ID) REFERENCES PUBLIC.EPISODE(ID) (3)" Referential integrity constraint violation: "FK50GHKTDAXMN68TBU6KAYVUX9S: PUBLIC.LIST_OF_EPISODES FOREIGN KEY(LIST_OF_EPISODES_ID) REFERENCES PUBLIC.EPISODE(ID) (3)";违反德contrainte: “FK50GHKTDAXMN68TBU6KAYVUX9S:PUBLIC.LIST_OF_EPISODES外键(LIST_OF_EPISODES_ID)参考PUBLIC.EPISODE(ID)(3)” 参照完整性约束违规:“FK50GHKTDAXMN68TBU6KAYVUX9S:PUBLIC.LIST_OF_EPISODES外键(LIST_OF_EPISODES_ID)参考PUBLIC.EPISODE(ID)( 3)"; SQL statement: delete from episode where id=? SQL 语句:从情节中删除,其中 id=? [23503-200] [23503-200]

Why hibernate can't remove this object and update the list in Webtoon class ?为什么 hibernate 不能删除这个对象并更新 Webtoon 类中的列表?

尝试将 FetchType 从 EAGER 更改为 LAZY

Referential integrity is a property of data stating that all its references are valid.引用完整性是数据的一种属性,表明其所有引用都是有效的。 In the context of relational databases, it requires that if a value of one attribute (column) of a relation (table) references a value of another attribute (either in the same or a different relation), then the referenced value must exist.在关系数据库的上下文中,它要求如果关系(表)的一个属性(列)的值引用另一个属性的值(在相同或不同的关系中),则引用的值必须存在。

From the error PUBLIC.LIST_OF_EPISODES FOREIGN KEY(LIST_OF_EPISODES_ID) REFERENCES PUBLIC.EPISODE(ID) you can clearly see that PUBLIC.EPISODE(ID) is referenced as a foreign key in PUBLIC.LIST_OF_EPISODES table so you cannot delete a parent unless you delete a child element.从错误PUBLIC.LIST_OF_EPISODES FOREIGN KEY(LIST_OF_EPISODES_ID) REFERENCES PUBLIC.EPISODE(ID) 中,您可以清楚地看到 PUBLIC.EPISODE(ID) 被引用为 PUBLIC.LIST_OF_EPISODES 表中的外键,因此除非您删除父项,否则您无法删除父项子元素。

Try using @JoinColumn instead of using @CollectionTable(name= "list_of_episodes")尝试使用 @JoinColumn 而不是使用 @CollectionTable(name="list_of_episodes")

To correct this issue, I first changed the uni-directional relation to bi-directional.为了解决这个问题,我首先将单向关系改为双向关系。 In Webtoon I have :在 Webtoon 中,我有:

@OneToMany(cascade = CascadeType.ALL, mappedBy="webtoon", orphanRemoval = true)
@CollectionTable(name= "list_of_episodes")
List<Episode> listOfEpisodes = new ArrayList<>();

In Episode, I added a webtoon attribute在Episode中,我添加了一个webtoon属性

@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="webtoon_id")
Webtoon webtoon;

Because, it is lazy...I could not get the list as if I'm using eager, so I added a join select in my repository.因为,它很懒惰......我无法获得列表,就像我在使用eager一样,所以我在我的存储库中添加了一个连接选择。 And also when I delete, I have the cascade delete.而且当我删除时,我有级联删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从 hibernate 中与 cascadeType.ALL 的 OneToMany 关系中删除时出现参照完整性约束违规 - Getting referential integrity constraint violation while trying to delete from OneToMany relation with cascadeType.ALL in hibernate Hibernate / H2 @OneToMany移除子代时违反“参照完整性约束”? - Hibernate/H2 @OneToMany “Referential integrity constraint violation” on remove of child? 所有者删除时违反参照完整性约束(OneToMany单向) - Referential integrity constraint violation on owner delete (OneToMany unidirectional) 尝试删除时违反参照完整性约束 - Referential integrity constraint violation when trying to delete 更新和/或删除时违反 Hibernate H2 参照完整性约束 - Hibernate H2 Referential Integrity Constraint Violation on Update and/or Remove Hibernate OneToMany完整性约束违规:外键没有父级 - Hibernate OneToMany integrity constraint violation: foreign key no parent JPA 2:在保存具有无方向OneToMany关系的实体时违反了引用完整性约束 - JPA 2: Referential integrity constraint violation while saving entity with undirectional OneToMany relationship H2参照完整性约束违反 - H2 Referential integrity constraint violation 无继承关系的参照完整性约束违规 - Referential integrity constraint violation for none inheritance relationship 如何防止在测试中违反参照完整性约束? - How to prevent referential integrity constraint violation in tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM