| Author |
Does book has an example on parent-child relation?
|
Varun Chopra
Ranch Hand
Joined: Jul 10, 2008
Posts: 204
|
|
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) - Mock Tests
|
 |
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: 204
|
|
|
Thanks Srinivas.
|
 |
 |
|
|
subject: Does book has an example on parent-child relation?
|
|
|