Hibernate 映射一对多,奇怪的例子

发布于 2024-10-12 21:20:30 字数 421 浏览 4 评论 0原文

我们正在尝试建立一对多的关系。为了理解它是如何工作的,我们偶然发现了 这个例子,它展示了如何做到这一点。

该示例显示了一个合理的 ER 图,但一般来说,在一对多关系中不需要辅助表。我们可以将 StudentId 列插入到 Phone 表中,并通过此标识符连接两个表来实现链接。相比之下,在该示例中,他们需要连接三个表。

我们想知道这种行为是否是由一些使 Hibernate 易于管理的良好实践证明是合理的,或者这是否只是撰写本文的人的错误。

有什么想法吗?

We are trying to build a One-to-many relation. Trying to understand how this works, we stumbled across this example, which shows how to do it.

The example shows a reasonable ER diagram, but generally speaking in a One-to-many relation one doesn't need an auxiliary table. We could insert a StudentId column into the Phone table and achieve the linking by joining the two tables through this identifier. In the example, by contrast, they need to join three tables instead.

We are wondering if such a behaviour is justified by some good practice which makes the management easy for Hibernate or if it's just an error of the guy who wrote the article.

Any thoughts?

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

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

发布评论

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

评论(1

薄荷港 2024-10-19 21:20:30

我找到了答案,看来这正是一种解决方法......
http://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Join_Table

通常最好在Java中定义ManyToOne反向引用,如果你不能或不想这样做,那么你可以使用中间连接表来存储关系。这类似于 ManyToMany 关系,但如果您向目标外键添加唯一约束,则可以强制它是 OneToMany。

I've found an answer, and it seems it's exactly a sort of workaround...
http://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Join_Table

Normally it is best to define the ManyToOne back reference in Java, if you cannot or don't want to do this, then you can use a intermediate join table to store the relationship. This is similar to a ManyToMany relationship, but if you add a unique constraint to the target foreign key you can enforce that it is OneToMany.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文