Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Head First EJB

 
Jeff Haynes
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a little confused on the answer to the following question from this book.

Given the container-managed unidirectional relationship:

Foo (0-1) -> Bar (0-1)

And the object relations:

f1->b1
f2->b2

What will be true after the following code runs?

f2.setBar(f1.getBar());

a.) f1.getBar() == null
b.) b2.getFoo() == null
c.) b1.getBar() == null
e.) none of the above



Here is my thought process:

I understand this to mean..

Foo (0-1) -> Bar (0-1)

For Foo, it can have zero or 1 bar.
For each Bar, it can have zero or 1 foos.

AKA here is my table structure.

FOO TABLE
Foo PK, Foo Num, Bar PK (can be null)

Bar Table
Bar PK, Bar Num, Foo PK (can be null)



I understand this to mean..

f1->b1
f2->b2

F1 has a bar(called 1)
F2 has a bar(called 2)

So if this code is called
f2.setBar(f1.getBar());

You will have

F1 has a bar(called 1)
F2 has a bar(called 1)

What I am missing is where does it say that each bar has a foo?


The answer by the way is A


I think I just don't understand the syntax of the question. Any ideas on how I should read this?
 
Ugender Rekulampally
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,

See you have one to one relationship between your objects. And in one-to-one relation, the reference will be transferred(write over) not copy over.

in the first case f1-->b1
f2-->b2

after you execute f2.setBar(f1.getBar());

the scenario will be f2 --> b1
but not f1-->b1 or f1-->b2..it will be f1-->null (there is no bar object assigned to it now) since they have one to one relation ship. so if you call f1.getBar()you will get null.

So the answer is A.


Thanks,
Ugender
 
Jeff Haynes
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay tell me if I am wrong..

It is a 1-1 relationship between Foo and Bar

And

It is a 1-1 relationship between Bar and Foo

Given the information

F1->b1
F2->b2

And when you assign F2.setbar(f1.getBar())

Then this makes some sense as

F1->Null
F2->b1

Then shouldn't B2->Null be true too? Or is this answer wrong because you can't assume this?
 
Jeff Haynes
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why I ask this, is because on page 424 of the book there is a nice little diagram that shows what I thought was the same scenario.

<ejb-relationship-role-name>MovieBean</ejb-relationship-role-name>
<multiplicity>One</multiplicity>

<ejb-relationship-role-name>Trailer</ejb-relationship-role-name>
<multiplicity>One</multiplicity>


M1->TA
M2->TB


m1.setTrailer(M2.getTrailer())

Then you have this..

M1->TB

TA->null

M2->null


Maybe I am assuming to much here..

All thoughts are welcome!!

-Jeff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic