gpt4 book ai didi

hibernate 最佳做法: Avoiding Many-To-Many and 'exotic' relationships

转载 作者:行者123 更新时间:2023-12-03 14:15:20 30 4
gpt4 key购买 nike

hibernate 最佳实践指出,多对多关联很少见,应避免。

Do not use exotic association mappings:

Practical test cases for real many-to-many associations are rare. Most of the time you need additional information stored in the "link table". In this case, it is much better to use two one-to-many associations to an intermediate link class. In fact, most associations are one-to-many and many-to-one. For this reason, you should proceed cautiously when using any other association style.



一个基本且常见的情况是:用户可以在多个团队中,而一个团队可以有多个成员。

除了为联接表创建实体之外,还有使用@ManyToMany的替代方法吗?对于团队/成员,联接表中没有其他数据,因此让Team> TeamMembership> User不太实用。

最佳答案

使用@ManyToMany并没有本质上的错误,但是在实践中,您很少有机会使用它。通常,您需要在链接本身上具有其他属性,此时,该关系将成为其自身的实体。

根据我的经验,一个例子就是您描述的这种人/团队关系。我最初使用@ManyToMany,但是一旦我需要在关系上添加有效日期(某人在特定时间属于团队),就不得不将其转换为@OneToMany

关于 hibernate 最佳做法: Avoiding Many-To-Many and 'exotic' relationships,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18594234/

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