• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Owner of the RelationShip

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am totally confused by the concept of the owner of the relationship-

example-

Car.java content has
-------------------------
@ManyToMany
protected Set<CarItems> carItems;


CarBids.java content has
-------------------------
@ManyToOne
protected Car car;

Now i want to now out of these two classes, who is the owner of the relationship.

Book is confusing me.
Thanks in Advance.
 
Chaminda Amarasinghe
Ranch Hand
Posts: 404
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one to one - which holds others primary key
one to many (bi) - many side
many to many - either side
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chaminda,

i thought the concept of owner relationship lies in the fact on which side we do use the mappedBy attribute. Suppose-

So now the parent table will be CarBillingInfo entity because it is using the mappedBy attribute as the owning side of the relationship.
------------------
@OneToOne(mappedBy = "carBillingInfo", optional = false)
protected Car car;
------------------


it will be the child table Car.
--------------
@OneToOne
protected CarBillingInfo carBillingInfo;
----------------------


Please correct me if i am wrong.
 
Chaminda Amarasinghe
Ranch Hand
Posts: 404
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amandeep Singh:
Chaminda,

i thought the concept of owner relationship lies in the fact on which side we do use the mappedBy attribute. Suppose-

So now the parent table will be CarBillingInfo entity because it is using the mappedBy attribute as the owning side of the relationship.
------------------
@OneToOne(mappedBy = "carBillingInfo", optional = false)
protected Car car;
------------------


it will be the child table Car.
--------------
@OneToOne
protected CarBillingInfo carBillingInfo;
----------------------


Please correct me if i am wrong.


In One to One bidirectional your understanding is not correct, mappedBy att. defines according to the relationship exist. mappedBy att. does not define the owner of O2O bi relationship

In O2O bi, ownership is defined by its table structure. ie. by who holds others primary key as his foreign key

Lets get an example,
Employee has exactly one PersonalInfomationFile and vise versa

Employee Table
---------
id
personalInfomationFile (fk PersonalInfomationFile.id)// this is id field of PersonalInfomationFile
//and other columns

PersonalInfomationFile Table
---------
id
//and other columns

In this case Employee table holds PersonalInfomationFile's id as a foreign key. So Employee is the owner. Therefor you must put mappedBy in PersonalInfomationFile class

This explanation is true for O2M as well. Many side holds one side's id always as a foreign key. So that's why many side always becomes the owner.

in manay to many either table doesnt keep other's id. so you can define owner in either side.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chaminda for the reply.

So again my question comes here as-

How you define the ownership of the table.
Do we define ownership by mappedBy?

let give example-

------------------
@OneToOne(mappedBy = "carBillingInfo", optional = false)
protected Car car;
------------------



--------------
@OneToOne
protected CarBillingInfo carBillingInfo;
----------------------

these are two table's made and they have o2o relationship.
Now how it will be concluded who is the parent table and the child table?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a bidirectional relationship, the mappedBy element designates the property or field in the entity that is the owner of the relationship.
 
Chaminda Amarasinghe
Ranch Hand
Posts: 404
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amandeep Singh:
Thanks Chaminda for the reply.

So again my question comes here as-

How you define the ownership of the table.
Do we define ownership by mappedBy?

let give example-

------------------
@OneToOne(mappedBy = "carBillingInfo", optional = false)
protected Car car;
------------------



--------------
@OneToOne
protected CarBillingInfo carBillingInfo;
----------------------

these are two table's made and they have o2o relationship.
Now how it will be concluded who is the parent table and the child table?


Hi Amandeep,

In short, Ownership is defined by the table structure, not by the mappedBy att.

If Car table keeps CarBillingInfo's id for relationship the owner is Car. else If CarBillingInfotable keeps Car's id for relationship the owner is CarBillingInfotable.

usually m2m bi doesnot have an exact owner. so we can treat mappedBy can define owner for ONLY m2m.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Chaminda and Christophe
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic