Hi,
I am using the following HQL to find out the product, product Image and related product from 3 different tables on certain condiction:
From Product as product
left outer join fetch product.productImage
left outer join fetch product.relatedProduct
where <some conditions here>
In my test, I try to print out the following information
1 prodct name // this is OK
2. relatedProduct // this is OK
3. product image name // ERROR: net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no Session
Now if i change my query to (change the order of left outer join):
From Product as product
left outer join fetch product.relatedProduct
left outer join fetch product.productImage
where <some conditions here>
The print result will be :
1 prodct name // this is OK
2. relatedProduct // ERROR: net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no Session
3. product image name // this is OK
========
It seems like only the second left join will take effect. Although from hibernate's print out sql, i can see both two have been joined:
Hibernate: select product0_.id as id0_, producti1_.id as id1_, relatedp3_.id as id2_, product0_.code as code0_, product0_.name as name0_, product0_.vendor_code as vendor_c4_0_, product0_.supplier_code as supplier5_0_, product0_.description as descript6_0_, product0_.warranty_info as warranty7_0_, product0_.quantity_in_stock as quantity8_0_, product0_.quantity_in_order as quantity9_0_, product0_.product_pricing_rate as product10_0_, product0_.cost as cost0_, product0_.rrp as rrp0_, product0_.price as price0_, product0_.on_sale as on_sale0_, product0_.special_price as special15_0_, product0_.special_start as special16_0_, product0_.special_end as special17_0_, product0_.bonus_point as bonus_p18_0_, product0_.redeemable as redeemable0_, product0_.redeem_point as redeem_20_0_, product0_.product_url as product21_0_, product0_.product_driver_url as product22_0_, product0_.weight as weight0_, product0_.dimension as dimension0_, product0_.default_pricing_rate as default25_0_, product0_.supplier_id as supplie26_0_, product0_.category_id as categor27_0_, product0_.vendor_id as vendor_id0_, producti1_.image_content as image_co2_1_, producti1_.image_name as image_name1_, producti1_.thumbnail_content as thumbnai4_1_, producti1_.product_id as product_id1_, relatedp3_.source_id as source_id2_, relatedp3_.dest_id as dest_id2_, relatedp3_.id as id__, relatedp3_.source_id as source_id__ from product product0_ left outer join product_image producti1_ on product0_.id=producti1_.product_id left outer join product product2_ on producti1_.product_id=product2_.id left outer join related_product relatedp3_ on product2_.id=relatedp3_.source_id where (1=1 )
===========================
Can you tell me what's wrong with my query?
thanks
ganfeng
|