• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Does book has an example on parent-child relation?

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Srinivas.
 
reply
    Bookmark Topic Watch Topic
  • New Topic