gpt4 book ai didi

java - 在不一致的数据库上 hibernate @ManyToOne

转载 作者:行者123 更新时间:2023-11-30 11:58:26 25 4
gpt4 key购买 nike

假设我有两个表,employee 和 department,其中 emp 有一个指向 dept 的 @ManyToOne 键。现在我希望能够从 dept 表中删除行,但保持 emp 记录指向它;基本上是保存关系,以便在重新创建部门表时恢复关系。 (这实际上发生在我们的系统中,但不是通过 hibernate ,而是通过使用组合的、可复制的键)。

问题是:@ManyToOne 关系是否会 hibernate 崩溃,这些关系似乎存在但在关系的@One 部分中没有记录? (基本上是不一致的数据库状态)。

我可能应该通过删除@ManyToOne 关系并简单地将外键映射到 String 左右来解决这个问题。我只是想知道我们是否可以把它拉下来以保持这种关系......

最佳答案

Now I want to be able to delete rows from the dept table but keep the emp records pointing to it;

然后你必须从逻辑上而不是物理上删除它们(FK 约束实际上会阻止你删除部门)。

Will hibernate crash on @ManyToOne relationships which seem to be there but with no record in the @One part of the relationship?

你没有参照完整性吗(见上文)?但是,假设您破坏了完整性……也许您可以加载员工,但某些东西会在某个时候崩溃(加载关联时,如果不是之前的话)。

I probably should solve this by removing the @ManyToOne relationship and simply map the foreign key to a String or so. I just wondered whether we can pull it off to leave the relationship in place..

按逻辑删除部门。

关于java - 在不一致的数据库上 hibernate @ManyToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3179354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com