使用@OneToMany和@ManyToMany之间的区别 [英] Difference between using @OneToMany and @ManyToMany

查看:1242
本文介绍了使用@OneToMany和@ManyToMany之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解 @OneToMany @ManyToMany 之间的区别时遇到了一些麻烦。当我使用 @OneToMany 时,它默认创建JoinTable,如果添加mappedBy属性,则两个实体之间将具有双向关系。

I am having some trouble understanding the difference between @OneToMany and @ManyToMany. When I use @OneToMany it defaults to create a JoinTable and if you add the mappedBy attribute you will have bidirectional relationship between two entities.

我有问题,可能属于许多类别和一个类别可能属于许多问题。我不明白我是否应该使用 @ManyToMany @OneToMany 因为对我而言似乎完全相同,但它可能不是。

I have a Question that may belong to many Categories, and one Category may belong to many Questions. I don't understand if I should use @ManyToMany or @OneToMany because for me it seems exactly the same thing, but it is probably not.

有人可以解释一下吗?

推荐答案

嗯,区别在于在设计中你试图反映使用对象。

Well, the difference is in the design you're trying to reflect using objects.

在你的情况下,每个问题都可以分配给多个类别 - 这是 @ * ToMany 关系的标志。现在你必须决定:

In your case, every Question can be assigned to multiple Categories - so that's a sign of @*ToMany relationship. Now you have to decide if:


  • 每个类别只能有一个<$分配给它的c $ c>问题(这将导致唯一约束,这意味着没有其他类别可以引用相同的问题) - 这将是 @OneToMany 关系,

  • 每个类别可以有多个<分配给它的code>问题(类别表中没有唯一约束) - 这将是 @ManyToMany relationship。

  • each Category can have only one Question assigned to it (it will result in a unique constraint which means that no other Category can refer the same Question) - this will be @OneToMany relationship,
  • each Category can have multiple Questions assigned to it (there will be no unique constraint in the Category table) - this will be @ManyToMany relationship.

@OneToMany(问题 - >类别)

只有在使用 @JoinTable 明确定义或当它是一个时,才可以通过连接表来表示此关系单向关系,其中拥有方是一方(这意味着在问题实体中,您拥有<$ c $的集合c>类别,但在分类中s 您没有任何对问题的引用。

This relationship can be represented by join table only if you explicitly define so using @JoinTable or when it is a unidirectional relationship in which the owning side is the 'One' side (it means that in the Question entity you have a collection of Categories, but in the Categories you don't have any reference to the Question).

如果你想一想,使用连接表似乎很合理。没有其他方法DBMS可以在 Question 表中的一行与 Categories 表中的多行之间保存连接。

If you think about it, it seems quite reasonable that the join table is used. There is no other way the DBMS could save a connection between one row in Question table with multiple rows in Categories table.

但是,如果您想建模双向关系,您需要指定类别('很多'这是关系的拥有方。在这种情况下,DBMS可以在 Category 表中创建一个带有外键的连接列,因为每个 Category 行都可以连接只有一个问题

However, if you would like to model a bidirectional relationship you need to specify that the Category ('Many' side) is the owning side of the relationship. In this case the DBMS can create a join column with foreign key in the Category table because each Category row can be connected with only one Question.

这样你就没有任何连接表而是简单的外键(仍然,如开头所指,您可以强制使用 @JoinTable 创建连接表。

In this way you don't have any join table but simple foreign keys (still, as pointed at the beginning, you can force to create the join table using @JoinTable).

@ManyToMany

此关系必须表示为连接表。它基本上与单向 @OneToMany 关系非常相似,但在这种情况下,您可能有多行来自问题已加入来自 Categories 的多行。

This relationship must be represented as a join table. It basically works very similar to the unidirectional @OneToMany relationship, but in this case you may have multiple rows from Question joined with multiple rows from Categories.

这篇关于使用@OneToMany和@ManyToMany之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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