JPA:am:n 关系中哪一方应该是拥有方?

发布于 2024-09-14 12:19:38 字数 190 浏览 4 评论 0原文

举例来说,我有两个实体:ArticleTag(就像在典型的博客中一样)。每篇文章可以有多个标签,每个标签又可以被多篇文章使用,因此是经典的 m:n 关系。

我需要使用 JPA 指定拥有方。但哪一方应该是拥有一方呢?一篇文章不依赖于某个标签,反之亦然。是否有经验法则来确定哪一方应该是拥有一方?

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.

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?

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

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

发布评论

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

评论(6

风吹短裙飘 2024-09-21 12:19:38

每个双向关系都需要 JPA 中的拥有方。在 ManyToMany 的特定情况下:

  • @JoinTable 在关系的拥有方指定。
    • 拥有方任意,您可以选择两个实体中的任何一个作为所有者。

来自 JPA 规范:

9.1.26 ManyToMany 注解

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

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

  • @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.

From the JPA specification:

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.

最冷一天 2024-09-21 12:19:38

您可以通过考虑希望更新关联的位置来选择拥有方。您只能在一个地方(拥有方)更新多对多关联。因此,选择取决于您想要如何管理/更新关联字段。

You choose the owning side by considering where you want the association be updated. You can update de ManyToMany association in only one place( the owning side). So the choice depend on how you want to manage/update your association fields.

情未る 2024-09-21 12:19:38

我的观点:

这取决于你的业务。哪个实体在您的业务中更重要。

在你的例子中,我认为文章应该是拥有方,

my point of view:

it depends on your business. which entity is more important in your business.

in your example, i think Article should be owning side,

最舍不得你 2024-09-21 12:19:38

另外值得一提的是,在 JPA 中,拥有方并不意味着包含方或拥有其他实体的一方。有关此内容的更多信息,请参见:在双向 JPA OneToMany/ManyToOne 关联,“关联的反面”是什么意思?

Also it worths to mention that in JPA the owning-side does not imply the containing side or the side that owns the other entities. More about this here: In a bidirectional JPA OneToMany/ManyToOne association, what is meant by "the inverse side of the association"?

牵你手 2024-09-21 12:19:38

每当存在 M:N 映射(即存在双向映射)时,我们都会在代码中使用 @ManyToMany 和 @JoinTable 。

要回答“哪一方应该拥有关系”这个问题,请回到您创建的模型以及数据应如何存储在数据库中。

通常,更改仅从关系的所有者端传播到数据库。让我按照你的例子解释一下,

有两个表/模型/POJO,ArticleTag

每当发布帖子时,都会与标签建立关系。

或者,每当出版图书时,都会与作者建立关系。

因此,在您的情况下,@JoinColumn 应该放在 Post 中。

Whenever there is an M:N mapping i.e. there is a bidirectional mapping we use @ManyToMany and @JoinTable in our code.

To answer this question "Which side should own the relationship" goes back to models you create and how the data should be stored in the database.

Generally, the changes are only propagated to the database from the owner side of the relationship. Let me explain as per your example,

There are two tables / models / POJOs, Article and Tag.

Whenever a Post is posted, a relation with the Tags is made.

Or, Whenever a Book is published, a relation with the Author is made.

So, the @JoinColumn should go in Post in your case.

蓬勃野心 2024-09-21 12:19:38

在 MHO 中,这是需要 @ManyToMany 关系的典型情况。

如果您使用连接表,您可以在您的文章类中包含类似的内容。

@ManyToMany 
@JoinTable(name="TAG_ARTICLE", 
      joinColumns=@JoinColumn(name="ARTICLE_ID"),
      inverseJoinColumns=@JoinColumn(name="TAG_ID"))
private Collection<Tag> tags;

然后在你的 Tag 类中

@ManyToMany(mappedBy="tags")
private Collection<Article> articles;

In MHO this is a typical case where a @ManyToMany relationship is needed.

If you use a Join Table, you can have in your Article class something like.

@ManyToMany 
@JoinTable(name="TAG_ARTICLE", 
      joinColumns=@JoinColumn(name="ARTICLE_ID"),
      inverseJoinColumns=@JoinColumn(name="TAG_ID"))
private Collection<Tag> tags;

Then in your Tag class

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