一对多,多对一和多对多的区别?

好,所以这可能是一个微不足道的问题,但我很难形象化和理解差异以及何时使用每个差异。 对于单向和双向映射等概念如何影响一对多/多对多的关系,我也有点不清楚。 我现在正在使用Hibernate,所以任何与ORM相关的解释都会有所帮助。

举一个例子,假设我有以下设置:

public class Person{ private Long personId; private Set<Skill> skills; //Getters and setters } public class Skill{ private Long skillId; private String skillName; //Getters and setters } 

那么在这种情况下我会有什么样的映射? 这个具体例子的答案是肯定的赞赏,但我也真的很喜欢什么时候使用一对多和多对多的概述,何时使用连接表与连接列和单向与双向。

一对多(One-to-Many) :一个人拥有很多技能,一个技能不会在人与人之间重复使用

  • 单向 :一个人可以通过它的Set直接引用技能
  • 双向 :每个“孩子”技能有一个单一的指针备份到人(这是没有显示在您的代码)

多对多 :一个人有很多技能,一个技能被重复使用,

  • 单向 :一个人可以通过它的Set直接引用技能
  • 双向 :技能有一套与之相关的人员。

在一对多关系中,一个对象是“父母”,一个是“孩子”。 父母控制着孩子的存在。 在“多对多”中,任何一种types的存在都依赖于它们之外的某些东西(在更大的应用环境中)。

您的主题(领域)应该决定这种关系是一对多还是多对多,但是我发现使关系成为单向或双向关系是一个工程决策,它将内存,处理,性能等等

可以混淆的是,多对多双向关系不需要是对称的! 也就是说,一群人可以指向一种技能,但是技能不需要和那些人联系起来。 通常情况下,但这种对称性不是必需的。 以爱为例,它是双向的(“我爱”,“爱我”),但往往是不对称的(“我爱她,但她不爱我”)!

所有这些都得到了Hibernate和JPA的很好的支持。 请记住,在pipe理双向多对多关系时,Hibernate或任何其他ORM都不会提供维护对称性的窍门……这一切都取决于应用程序。

看起来每个人都在回答One-to-many VS Many-to-many

One-to-manyMany-to-oneMany-to-Many的区别在于:

One-to-manyMany-to-one是一个angular度问题UnidirectionalBidirectional不会影响映射,但会影响您如何访问数据。

  • One-to-manymany方面都会保留一方的参照。 一个很好的例子是“一个人有很多技能”。 在这种情况下, Person是一方, Skill是多方面的。 表格中会有一个person_id字段。

单向的 Person类中将有List<Skill> skillsSkill不会有Person person 。 在双向中,这两个属性都被添加,并允许您访问给定技能的人员(即skill.person )。

  • Many-to-one的许多方面将是我们的参考点。 例如,“用户有一个地址”。 在我们的系统中,许多用户可能共享一个地址(例如,许多人可能共享相同的块号)。 在这种情况下, users表中的address_id列将被多个users行共享。 在这种情况下,我们说usersaddressesMany-to-one关系。

单向User将有Address address双向将在Address类中有一个额外的List<User> users

  • 在各方Many-to-Many成员中可以参照任意数量的另一方成员。 为了达到这个目的,使用查找表 。 例如医患关系。 医生可以有很多病人,反之亦然。

1)圈子是实体/ POJO /豆

2)deg是图中的度数的缩写(边数)

PK =主键,FK =外键

注意一边的程度和名称之间的矛盾。 许多对应于度数= 1,而对应于度数> 1。

一对多多对一的插图

看看这篇文章: 映射对象关系

映射时需要关注两类对象关系。 第一类是基于多样性,它包括三种types:

 *One-to-one relationships. This is a relationship where the maximums of each of its multiplicities is one, an example of which is holds relationship between Employee and Position in Figure 11. An employee holds one and only one position and a position may be held by one employee (some positions go unfilled). *One-to-many relationships. Also known as a many-to-one relationship, this occurs when the maximum of one multiplicity is one and the other is greater than one. An example is the works in relationship between Employee and Division. An employee works in one division and any given division has one or more employees working in it. *Many-to-many relationships. This is a relationship where the maximum of both multiplicities is greater than one, an example of which is the assigned relationship between Employee and Task. An employee is assigned one or more tasks and each task is assigned to zero or more employees. 

第二类是基于方向性的,它包含两种types,单向关系和双向关系。

 *Uni-directional relationships. A uni-directional relationship when an object knows about the object(s) it is related to but the other object(s) do not know of the original object. An example of which is the holds relationship between Employee and Position in Figure 11, indicated by the line with an open arrowhead on it. Employee objects know about the position that they hold, but Position objects do not know which employee holds it (there was no requirement to do so). As you will soon see, uni-directional relationships are easier to implement than bi-directional relationships. *Bi-directional relationships. A bi-directional relationship exists when the objects on both end of the relationship know of each other, an example of which is the works in relationship between Employee and Division. Employee objects know what division they work in and Division objects know what employees work in them. 

这可能需要一个多对多的关系,如下所示

 public class Person{ private Long personId; @manytomany private Set skills; //Getters and setters } public class Skill{ private Long skillId; private String skillName; @manyToMany(MappedBy="skills,targetClass="Person") private Set persons; // (people would not be a good convenion) //Getters and setters } 

你可能需要定义一个joinTable + JoinColumn,但它也可能没有…

首先,阅读所有的细则。 请注意,NHibernate(因此,我假设,Hibernate)关系映射与DB和对象图映射有一个有趣的对应。 例如,一对一关系通常以多对一的关系来实现。

其次,在我们告诉你如何编写你的O / R地图之前,我们必须看到你的数据库。 特别是一个单一的技能可以被多个人所拥有? 如果是这样,你有一个多对多的关系; 否则,它是多对一的。

第三,我不想直接实现多对多的关系,而是在你的领域模型中build模“连接表” – 也就是把它看作一个实体,就像这样:

 class PersonSkill { Person person; Skill skill; } 

那你有没有看到你有什么? 你有两个一对多的关系。 (在这种情况下,Person可能拥有一组Person技能,但不会有技能集合)。但是,有些人会倾向于使用多对多的关系(人与技能之间)。 这是有争议的。

第四,如果你确实有双向关系(例如,Person不仅有一个技能集合,而且技巧还有一个人的集合),NHibernate不会在你的BL中强制执行双向性。 它只能理解为了持久目的的关系的双向性。

第五,在一个对象(集合映射)中,多对一的在NHibernate中正确使用(我假设Hibernate)要容易得多。

祝你好运!