What is the "own side" in the ORM mapping?

What does owning party mean? What is the explanation with some comparison examples ( from one to many, from one to one, from several to one )?

The following text is an excerpt from the description of @OneToOne in the Java EE 6 documentation. You can see the concept of ownership .

Defines a unique relationship with another entity that has one-to-one multiplicity. It is not normal to specify the associated target object explicitly, since it can usually be inferred from the type of the object being referenced. If the relationship is bidirectional, the non-owner must use the mappedBy OneToOne annotation element to indicate the relationship field or owner property.

+79
java mapping orm hibernate jpa
May 01 '10 at 11:11
source share
2 answers

You can imagine that the owner is an entity that has a link to another. In your passage, you have a one-to-one relationship. Since this is a symmetric relation, you get the result that if object A is connected to object B, then vice versa. True

This means that storing a reference to object B in object A and saving a reference to object A in object B will be redundant: why do you choose which object "owns" another that has a link to it.

When you have a one-to-many relationship, the objects associated with the large part will belong to the party, otherwise you will have to store many links from one object to many. To avoid this, each object in the second class will have a pointer to the one to which they refer (so that they are property).

For the many-to-many relationship, since in any case you need a separate mapping table, there will be no own side.

In conclusion, the owner is an entity that has a link to another.

+94
May 01 '10 at 11:24
source share

Why the concept of belonging is necessary:

The idea that the bidirectional relation belongs to the party arises because there are no bidirectional relations in relational databases, for example, in the case of objects. In databases, we have only one-way relationships - foreign keys.

What is the reason for the name "owning party"?

The private side of a relationship monitored by Hibernate is the side of the relationship that owns the foreign key in the database.

What is the problem that the idea of ​​owning a party solves?

Take an example of two objects displayed without declaring their own side:

@Entity @Table(name="PERSONS") public class Person { @OneToMany private List<IdDocument> idDocuments; } @Entity @Table(name="ID_DOCUMENTS") public class IdDocument { @ManyToOne private Person person; } 

From the point of view of OO, this comparison defines not one bi-directional relationship, but two separate unidirectional relationships.

The mapping would not only PERSONS and ID_DOCUMENTS , but also create the third PERSONS_ID_DOCUMENTS link PERSONS_ID_DOCUMENTS :

 CREATE TABLE PERSONS_ID_DOCUMENTS ( persons_id bigint NOT NULL, id_documents_id bigint NOT NULL, CONSTRAINT fk_persons FOREIGN KEY (persons_id) REFERENCES persons (id), CONSTRAINT fk_docs FOREIGN KEY (id_documents_id) REFERENCES id_documents (id), CONSTRAINT pk UNIQUE (id_documents_id) ) 

Pay attention to the primary key pk only on ID_DOCUMENTS . In this case, Hibernate tracks both sides of the relationship independently: if you add a document to the Person.idDocuments relationship, it inserts an entry into the PERSON_ID_DOCUMENTS association PERSON_ID_DOCUMENTS .

On the other hand, if we call idDocument.setPerson(person) , we change the foreign key person_id to the table ID_DOCUMENTS . Hibernate creates two unidirectional (foreign keys) relationships in the database to implement one bi-directional relationship of the object.

How the concept of the decisive side solves the problem:

In many cases, we want to use only the foreign key in the ID_DOCUMENTS table in relation to PERSONS and an additional association table.

To solve this problem, we need to configure Hibernate to stop tracking changes in relation to Person.idDocuments . Hibernate should only track the other side of the IdDocument.person relationship, and for this we add mappedBy :

 @OneToMany(mappedBy="person") private List<IdDocument> idDocuments; 

What does this mean: mappedBy?

This means something like: "modifications on this side of the relationship are already displayed using the IdDocument.person relationship on the other side, so there is no need to track it here separately in the additional table.

Are there any gotcha effects?

Using mappedBy . If we call only person.getDocuments().add(document) , the foreign key in ID_DOCUMENTS will be bound to NOT to the new document, because this is not the ownership / tracked side of the relationship!

To associate a document with a new person, you need to explicitly call document.setPerson(person) , because this is the owner side .

When using mappedBy, the developer needs to know what the owner is and update the right side of the relationship in order to make the new relationship persist in the database.

+102
Jan 11 '14 at 22:22
source share



All Articles