Ogrish Man Ogrish Man - 3 months ago 27
Java Question

What is the "owning side" in an ORM mapping?

I'm new to JPA. Now I have a question: what exactly does the owning side mean? I only have a rough idea of it. What is an explanation with some mapping examples (one to many, one to one, many to one)?

PS: 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.


You can imagine that the owning side is the entity that has the reference to the other one. In your excerpt, you have an one-to-one relationship. Since it's a symmetric relation, you'll end up having that if object A is in relation with object B then also the vice-versa is true.

This means that saving into object A a reference to object B and saving in object B a reference to object A will be redundant: that's why you choose which object "owns" the other having the reference to it.

When you have got an one-to-many relationship, the objects related to the "many" part will be the owning side, otherwise you would have to store many references from a single object to a multitude. To avoid that, every object in the second class will have a pointer to the single one they refer to (so they are the owning side).

For a many-to-many relationship, since you will need a separate mapping table anyway there won't be any owning side.

In conclusion the owning side is the entity that has the reference to the other.