wood burning stoves 2.0*
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
Author

Advantages of bidirectional @onetomany over unidireational

sachin pachpute
Ranch Hand

Joined: Aug 05, 2004
Posts: 35
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

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: http://aspose.com/file-tools
 
subject: Advantages of bidirectional @onetomany over unidireational
 
Similar Threads
org.hibernate.HibernateException: identifier of an instance of Address was altered from 2 to 3
one to many unidirectional and bidirectional
EJB3 Entity Relationship
EJB Relation Question
Persistence Example