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 Advantages of bidirectional @onetomany over unidireational Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Advantages of bidirectional @onetomany over unidireational" Watch "Advantages of bidirectional @onetomany over unidireational" New topic

Advantages of bidirectional @onetomany over unidireational

sachin pachpute
Ranch Hand

Joined: Aug 05, 2004
Posts: 35

I went through the hibernate doc and not able to figure out if I should use bidirectional or unidirectional one-to-many relationship.

Each product has three images. My requirement is, I want to get all the products, loop through it and only display 1st image (along with product details).

should i be using unidirectional or bidirectional one-to-many relationship? I went through the hibernate doc but still not clear.

-- Table schema

Expiry date

image url

Urvaksh Rogers

Joined: Jun 28, 2012
Posts: 2
Your requirement seems pretty clear, since you need to fetch Images from Product your association needs to be Product->Image only i.e. uni directional.

If you envision an additional scenario in which you fetch all the images, and want to show the Product for each image you would want to make your association bidirectional.

Please remember that if you make an association bi-directional, you will need to pick a owning side. In your schema I don't see any Foreign keys, however form your problem description I would think that IMAGE would have PRODUCT_ID to point to the product. In this case make Image the owner of the association.
I agree. Here's the link:
subject: Advantages of bidirectional @onetomany over unidireational
It's not a secret anymore!