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.
Best 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:
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
andID_DOCUMENTS
, but would also create a third association tablePERSONS_ID_DOCUMENTS
:Notice the primary key
pk
onID_DOCUMENTS
only. In this case Hibernate tracks both sides of the relation independently: If you add a document to relationPerson.idDocuments
, it inserts a record in the association tablePERSON_ID_DOCUMENTS
.On the other hand, if we call
idDocument.setPerson(person)
, we change the foreign key person_id on tableID_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
towardsPERSONS
and 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 relationIdDocument.person
, and to do so we add mappedBy:What does it mean mappedBy ?
Are there any GOTCHAs, consequences?
Using mappedBy, If we only call
person.getDocuments().add(document)
, the foreign key inID_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.