This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Owner of the RelationShip Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Owner of the RelationShip" Watch "Owner of the RelationShip" New topic
Author

Owner of the RelationShip

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
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.


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
one to one - which holds others primary key
one to many (bi) - many side
many to many - either side
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
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

Joined: May 17, 2006
Posts: 402
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

Joined: Jul 17, 2008
Posts: 844
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

In a bidirectional relationship, the mappedBy element designates the property or field in the entity that is the owner of the relationship.


[My Blog]
All roads lead to JavaRanch
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
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

Joined: Jul 17, 2008
Posts: 844
thanks Chaminda and Christophe
 
GeeCON Prague 2014
 
subject: Owner of the RelationShip