一对多,多对一和多对多的区别? [英] Difference Between One-to-Many, Many-to-One and Many-to-Many?

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

问题描述

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



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

  public class Person {
private long personId;
私人套装<技巧>技能;
//获得者和设置者
}

公共类技能{
private Long skillId;
私人字符串skillName;
// Getters and setters
}

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

解决方案

一对多:一个人有很多技能,技能不会在人s)


  • 单向:一个人可以通过其Set

  • 双向:每个孩子技能都有一个单指针备份到
    Person(这不会显示在您的代码中)


多对多:一个人拥有多种技能,一个技能在个人之间重复使用




  • 单向:人员可以通过其设置

  • 双向直接引用技能:技能有一套与t相关的人员



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



您的主题(域)应该决定这种关系是一对多还是多对多 - 但是,我发现使关系成为单向或双向关系是一种工程决策,可以将内存,处理,性能等进行权衡。



令人困惑的是,多对多双向关系不需要是对称的!也就是说,一群人可以指向一种技能,但是技能不需要与那些人联系起来。通常情况下,但这种对称性不是必需条件。以爱为例 - 它是双向的(I-Love,Loves-Me),但往往是不对称的(我爱她,但她不爱我)!

b
$ b

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


Ok so this is probably a trivial question but I'm having trouble visualizing and understanding the differences and when to use each. I'm also a little unclear as to how concepts like uni-directional and bi-directional mappings affect the one-to-many/many-to-many relationships. I'm using Hibernate right now so any explanation that's ORM related will be helpful.

As an example let's say I have the following set-up:

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
}

So in this case what kind of mapping would I have? Answers to this specific example are definitely appreciated but I would also really like an overview of when to use either one-to-many and many-to-many and when to use a join table versus a join column and unidirectional versus bidirectional.

解决方案

One-to-Many: One Person Has Many Skills, a Skill is not reused between Person(s)

  • Unidirectional: A Person can directly reference Skills via its Set
  • Bidirectional: Each "child" Skill has a single pointer back up to the Person (which is not shown in your code)

Many-to-Many: One Person Has Many Skills, a Skill is reused between Person(s)

  • Unidirectional: A Person can directly reference Skills via its Set
  • Bidirectional: A Skill has a Set of Person(s) which relate to it.

In a One-To-Many relationship, one object is the "parent" and one is the "child". The parent controls the existence of the child. In a Many-To-Many, the existence of either type is dependent on something outside the both of them (in the larger application context).

Your subject matter (domain) should dictate whether or not the relationship is One-To-Many or Many-To-Many -- however, I find that making the relationship unidirectional or bidirectional is an engineering decision that trades off memory, processing, performance, etc.

What can be confusing is that a Many-To-Many Bidirectional relationship does not need to be symmetric! That is, a bunch of People could point to a skill, but the skill need not relate back to just those people. Typically it would, but such symmetry is not a requirement. Take love, for example -- it is bi-directional ("I-Love", "Loves-Me"), but often asymmetric ("I love her, but she doesn't love me")!

All of these are well supported by Hibernate and JPA. Just remember that Hibernate or any other ORM doesn't give a hoot about maintaining symmetry when managing bi-directional many-to-many relationships...thats all up to the application.

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

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