例如,我有两个实体:Article
和 Tag
(就像在典型的博客中一样)。每篇文章可以有多个标签,每个标签可以被多篇文章使用,是经典的m:n关系。
我需要使用 JPA 指定拥有方。但是哪一方应该是拥有方呢?一篇文章不依赖于某个标签,反之亦然。是否有确定哪一方应成为拥有方的经验法则?
最佳答案
在 JPA 中,每个双向关系都需要一个拥有方。在 ManyToMany
的特殊情况下:
@JoinTable
在关系的拥有方指定。- 拥有方是任意,您可以选择两个实体中的任何一个作为所有者。
来自 JPA 规范:
9.1.26 ManyToMany Annotation
Every many-to-many association has two sides, the owning side and the non-owning, or inverse, side. The join table is specified on the owning side. If the association is bidirectional, either side may be designated as the owning side.
关于java - JPA:哪一方应该是 m:n 关系中的拥有方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3484325/