wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Does book has an example on parent-child relation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Does book has an example on parent-child relation?" Watch "Does book has an example on parent-child relation?" New topic
Author

Does book has an example on parent-child relation?

Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
Dear Srinivas

If a table has 2 columns:

productId, parentProductId

with records like

1, null
2, 1
3, 1
4, null
5, 4

which means 1 and 4 are top level products with no parent.
2 and 3 are sub-products under 1 and 5 is a sub-product under 4.

Does book cover an example on how to map this and get the child products?

regards
Varun

-Varun -
(My Blog) - Online Certifications - Webner Solutions
srinivas guruzu
author
Greenhorn

Joined: Jun 08, 2010
Posts: 20
Hi Varun,

This can be achieved by <many-to-one> mapping. Just that the parentProduct is the Product object itself. so the mapping for the parentProduct would
look something like
<many-to-one name="parentProduct" class="Product" column="PARENT_PRODUCT_ID" cascade="save-update"/>

To get all the child products for a parent product, you can use criteria with restrictions, which would be something like

criteria.add(Restrictions.eq("parentProduct.productId", new Integer(1)));

Here we are getting all child products for the parent product with product Id =1

Ofcourse, the book covers many-to-one mapping.

Regards

Srinivas
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
Thanks Srinivas.
 
Consider Paul's rocket mass heater.
 
subject: Does book has an example on parent-child relation?
 
Similar Threads
Example of Reflexive composition relation.
why no error?
I am lost , Help Me Please ...JTree
Question for variable initialization order
constructor