Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate : Bidirectional Association

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume one to many relationship exists between Item and Bid and is bidirectional. As we need to access them in either way,to save an item, we need to write :

bid.setItem(item);//Line 1
Set bidset = new HashSet();
bidset.add(bid);
item.setBids(bidset);// Line 2

CASE 1:
suppose i only comment line 1 and then save item :
a)it's inserting into ITEM table
b) It's inserting into BID table but the FK (ITEM_ID) is setting as null...

So this means i won;t be able to retrieve bids from this item.. ?


CASE 2:
suppose i only comment line 2 and then save bid :
a) insert into bid
b) insert into item
c) update bid to set FK (ITEM_ID) properly

Moreover i don't understand that why in case 2) it executed the update (c)
while in case 1) it doesn't ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic