Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Advantages of bidirectional @onetomany over unidireational

 
sachin pachpute
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

|---------------------|
PRODUCT
|---------------------|
id
number
Expiry date
|---------------------|


|---------------------|
IMAGE
|---------------------|
id
image url
|---------------------|

 
Urvaksh Rogers
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic