具有两个父类的子类上的 Hibernate 全部删除孤儿

发布于 2024-09-09 15:49:48 字数 267 浏览 0 评论 0原文

我正在研究一个问题,其中有两个“父”类 P 和 Q 将所有删除孤儿级联到“子”类 C。我在 Hibernate 中的直觉告诉我这确实是一个坏主意,我当代码删除 P 的实例(即 session.delete(myP); )时收到一条错误消息,该消息可能证实了这一点:

“已删除的对象将通过级联重新保存(从关联中删除已删除的对象):[C#1] “

当 P 的实例和 Q 的实例都可以充当同一个 C 实例的父类时,任何人都可以确认为单个子类拥有两个父类是一个坏主意吗?

谢谢!

I'm looking into a problem, where there are two "parent" classes, P and Q that cascade all-delete-orphan to a "child" class, C. My intuition in Hibernate tells this is really a bad idea and I am getting an error message that probably confirms this when the code deletes an instance of P (i.e. session.delete(myP); ):

"deleted object would be re-saved by cascade (remove deleted object from associations): [C#1]"

Can anyone confirm that having having two parent classes for a single child class is a bad idea when an instance of P and instance of Q can both act as the parent for the same instance of C?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2024-09-16 15:49:48

我的猜测是,如果您在同一个事务中同时拥有 P 和 Q,同时更新 P 并从 Q 中删除(反之亦然),那么这只会成为问题。双重父母身份只会给交易增加一层复杂性,但它仍然应该按照您期望的方式运作。

session.beginTransaction();
P p = loadP(); 
p.remove(c);
session.commit(); //okay

session.beginTransaction();
P p = loadP();
Q q = loadQ();
p.remove(c);
q.alter(c);
session.commit(); //boom

My guess is that is only going to be a problem if you have both P and Q in the same transaction, simultaneously updating P and deleting from Q (or vice versa). Double parenthood should just add a layer of complexity to the transaction but it should still work the way you would expect.

session.beginTransaction();
P p = loadP(); 
p.remove(c);
session.commit(); //okay

session.beginTransaction();
P p = loadP();
Q q = loadQ();
p.remove(c);
q.alter(c);
session.commit(); //boom
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文