File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate : Bidirectional Association Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate : Bidirectional Association" Watch "Hibernate : Bidirectional Association" New topic

Hibernate : Bidirectional Association

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 392
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();
item.setBids(bidset);// Line 2

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.. ?

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 ?
I agree. Here's the link:
subject: Hibernate : Bidirectional Association
jQuery in Action, 3rd edition