Reputation: 243
I have one to many relationship between Person and Address class. According to Hibernate doc many side is always the owning side of the relation, that is Many side should not define mappedBy element.
in my case one Person can have many address, so Person corresponds to one side and Address corresponds to many side. If i don't define mappedBy element in both classes which class becomes the owner of the relationship?
Is it always that the Owner table has the foreign key mapping?
Upvotes: 1
Views: 451
Reputation: 47163
Yes, the owning entity's table is always the one with the foreign key.
It is a shame that the writers of the JPA spec chose to call that entity the "owner" of the relationship. That name conflicts with the terminology around aggregation, which will often be in use at the same time. For example, in your case, the Person owns the Address (if you delete a Person, you would delete all their Addresses; if you delete an Address, you wouldn't delete the Person which owns it). However, it is the Address which owns the relationship between the Person and the Address.
Really, the "owning side" just means "the side which has the table which actually defines the foreign key used to record the relationship".
Upvotes: 3
Reputation: 42074
If you do not specify mappedBy, then you do not have bidirectional relationship. Instead you will have two unidirectional relationships.
In one-to-many relationship foreign key have to be in owner table or in separate join-table. You cannot have it in table of one side of one-to-many relationship. It doesn't make sense to have it in one side, because you cannot map many values (foreign keys referencing to table of many side) to the one column of one row with JPA.
Upvotes: 2