JPA:在m:n关系中哪一方应该是拥有一方? [英] JPA: which side should be the owning side in a m:n relationship?

查看:91
本文介绍了JPA:在m:n关系中哪一方应该是拥有一方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有两个实体:ArticleTag(就像在典型的博客中一样).每个文章可以有多个标签,并且每个标签可以被许多文章使用,因此这是一种经典的m:n关系.

Say, for example, I had two entities: Article and Tag (like in a typical blog). Each article can have many tags, and each tag can be used by many articles, so it is a classical m:n relationship.

我需要指定JPA的拥有方.但是哪一方应该是拥有方?文章不依赖特定标签,反之亦然.是否有经验法则来确定哪一方应该是拥有方?

I need to specify an owning side with JPA. But which side should be the owning side? An article doesn't depend on a certain tag and vice versa. Is there a rule of thumb for determining which side should be the owning side?

推荐答案

每个双向关系在JPA中都需要拥有一方.对于ManyToMany的特殊情况:

Every bidirectional relationship requires an owning side in JPA. In the particular case of ManyToMany:

    在关系的拥有方上指定了
  • @JoinTable.
    • 拥有方是任意,您可以选择两个实体中的任何一个作为所有者.
    • @JoinTable is specified on the owning side of the relationship.
      • the owning side is arbitrary, you can pick any of the two entities to be the owner.

      根据JPA规范:

      9.1.26 ManyToMany批注

      每个多对多关联都有两个 方面,拥有方和 非拥有或相反的一面.加盟 表是在拥有方指定的. 如果关联是双向的, 任何一方都可以指定为 拥有的一面.

      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.

      这篇关于JPA:在m:n关系中哪一方应该是拥有一方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆