Dormancy – trying to understand the importance of the owner of a one to many relationship in orm

Although my question is specific to the way entity relationships are described in the play framework using hibernate, I am sure this is a general concept

When we have a one - to - many relationship, we are always asked to specify the owner

So, for example, if we have 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 above code, the owning entity is phonenumber What are the advantages and disadvantages of either party as an owning entity?

I realize that when the owning entity is phonenumber, the relationship represented is manytoone, which will not generate a connection table. When the owner is person, the described relationship will be onetomany. In this case, the 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 some answers, but I hope there may be other points

Solution

For most ORM layers, you have the concept of deferred loading When creating a person object, the phone set is not loaded unless required Sometimes, the way you want to find data can also determine how you store data

It's like you want to first propose a person, then display the phone number on demand, and then keep the person reference on the phone First, you trigger a simple query to load personnel data, and then based on the (already loaded) person ID to find a phone number (another simple query)

However, in order to display the personnel telephone data at one time, you want to have a connection table. You can use the personnel ID as the key in the telephone table to load the data based on the personnel telephone connection table at one time Searching without relational tables can be expensive

But frankly, if you think of SQL instead of ORM, you will use the relational table: D every time

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>