"difference between one-to-many, many-to-one and many-to-many?" Code Answer

4

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.

By Frix on April 25 2022

Answers related to “difference between one-to-many, many-to-one and many-to-many?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.