An attempt to understand the importance of belonging to the one-to-many relationship in ORM

Despite the fact that my question is specifically formulated regarding how Entity relationships are displayed in a Play framework that uses Hibernate, I'm sure this is a general concept.

When we have a one-to-many relationship, we are always asked to indicate the side of the owner.

So, for example, if we had a one-to-many relationship between Person and PhoneNumber, we will write such code.

@Entity class Person { @OneToMany(mappedBy="person") public Set<PhoneNumber> phoneNumbers; } @Entity class PhoneNumber { @ManyToOne public Person person; } 

In the code above, your own organization is PhoneNumber. What are the pros and cons of any of the property parties?

I understand that if the owner is PhoneNUmber, then the presented relationship is ManyToOne, which will not lead to a connection table, whereas when the owner is Person, the designated connection will be OneToMany, in which case a relationship table will be created.

Is this the main reason for determining the owner or are there other reasons?

Update: I just realized that this thread provides part of the answer, but I hope there may be other points.

+7
source share
2 answers

With most ORM layers, you have the concept of lazy loading. When you create a Person object, it will not load phones unless asked. From time to time, how do you want the search data to also determine how you save it.

As if you first brought up a person, and then showed the phone numbers on demand, then keep the links to the phones in the phone in order. First you run a simple request to download user data, and then just look at the phone numbers based on the (already loaded) person.id (another simple request)

While for the simultaneous display of personal + telephone data you prefer to have a connection table in which you can simply download data based on the personalized table + personal phone table, using the person identifier as keys in the phone table, all at once, Here was It would be expensive to do a search without a relationship table.

But honestly, if you consider SQL instead of ORM, you will refer to the relationship table every time: D

+1
source

An important point to keep in mind is that the attitude of the owner is the one that is actually maintained in relation to conservation. Example:

  Person person = new Person(); PhoneNumber pn = new PhoneNumber(); pn.phone = "12345678"; person.phoneNumbers.add(pn); session.save(person); 

The relation does not save infact, if you reload the object from the database, you will not see any numbers. To actually add a relationship, you need to set the person on the owner side (PhoneNumber) and then save.

  // the relation is not saved Person loadedPerson = (Person)session.load(Person.class, person.id); System.out.println(loadedPerson.phoneNumbers.size()); // prints 0! pn.person = person; session.save(pn); loadedPerson = (Person)session.load(Person.class, person.id); System.out.println(loadedPerson.phoneNumbers.size()); // prints 1 
+6
source

All Articles