• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does book has an example on parent-child relation?

 
Varun Chopra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
srinivas guruzu
author
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Srinivas.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic