What exactly does the owning side mean? What is an explanation with some mapping examples (one to many, one to one, many to one)?
The following text is an excerpt from the description of @OneToOne in Java EE 6 documentation. You can see the concept owning side in it.
Defines a single-valued association to another entity that has one-to-one multiplicity. It is not normally necessary to specify the associated target entity explicitly since it can usually be inferred from the type of the object being referenced. If the relationship is bidirectional, the non-owning side must use the mappedBy element of the OneToOne annotation to specify the relationship field or property of the owning side.
Advertisement
Answer
Why is the notion of a owning side necessary:
The idea of a owning side of a bidirectional relation comes from the fact that in relational databases there are no bidirectional relations like in the case of objects. In databases we only have unidirectional relations – foreign keys.
What is the reason for the name ‘owning side’?
The owning side of the relation tracked by Hibernate is the side of the relation that owns the foreign key in the database.
What is the problem that the notion of owning side solves?
Take an example of two entities mapped without declaring a owning 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 a OO point of view this mapping defines not one bi-directional relation, but two separate uni-directional relations.
The mapping would create not only tables PERSONS
and ID_DOCUMENTS
, but would also create a third association table 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) )
Notice the primary key pk
on ID_DOCUMENTS
only. In this case Hibernate tracks both sides of the relation independently: If you add a document to relation Person.idDocuments
, it inserts a record in the association table PERSON_ID_DOCUMENTS
.
On the other hand, if we call idDocument.setPerson(person)
, we change the foreign key person_id on table ID_DOCUMENTS
. Hibernate is creating two unidirectional (foreign key) relations on the database, to implement one bidirectional object relation.
How the notion of owning side solves the problem:
Many times what we want is only a foreign key on table ID_DOCUMENTS
towards PERSONS
and not the extra association table.
To solve this we need to configure Hibernate to stop tracking the modifications on relation Person.idDocuments
. Hibernate should only track the other side of the relation IdDocument.person
, and to do so we add mappedBy:
@OneToMany(mappedBy="person") private List<IdDocument> idDocuments;
What does it mean mappedBy ?
This means something like: “modifications on this side of the relation are already Mapped By the other side of the relation IdDocument.person, so no need to track it here separately in an extra table.”
Are there any GOTCHAs, consequences?
Using mappedBy, If we only call person.getDocuments().add(document)
, the foreign key in ID_DOCUMENTS
will NOT be linked to the new document, because this is not the owning /tracked side of the relation!
To link the document to the new person, you need to explicitly call document.setPerson(person)
, because that is the owning side of the relation.
When using mappedBy, it is the responsibility of the developer to know what is the owning side, and update the correct side of the relation in order to trigger the persistence of the new relation in the database.