views:

165

answers:

2

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.

+1  A: 

See my answer to this question: http://stackoverflow.com/questions/2584521/in-a-bidirectional-jpa-onetomany-manytoone-association-what-is-meant-by-the-inv/2584591#2584591

Aaron Digulla
Thanks for your useful link.
Yousui
+2  A: 

You can imagine that the owning side is the entity that has the reference to the other one. In you 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, so 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.

Jack
Thank you for your clarification.
Yousui