Following JPQL is executing and returning expected resultset :
SELECT
fprm
FROM
FpRawMaterial fprm
JOIN FETCH
fprm.finishedProduct
JOIN FETCH
fprm.rawMaterial
ORDER BY
fprm.finishedProduct.productName
But getting above QueryException while joining one more table “uom” to JPQL :
SELECT
fprm
FROM
FpRawMaterial fprm
JOIN FETCH
fprm.finishedProduct
JOIN FETCH
fprm.rawMaterial
JOIN FETCH
fprm.finishedProduct.uom
ORDER BY
fprm.finishedProduct.productName
If I don’t join the “uom” table Hibernate will execute multiple sql queries to fetch uom for each product → N+1 issue.
How can I write the correct JPQL for following SQL query:
SELECT fprm.fp_id, fprm.rm_id, p1.fp_base_qty, u1.name 'Unit', p1.product_name AS 'FINISHED_PRODUCT',
p.product_name AS 'Raw_Material', fprm.rm_qty, u.name 'Unit' FROM erp_mini.fp_raw_material fprm
INNER JOIN erp_mini.product p ON fprm.rm_id = p.product_id
INNER JOIN erp_mini.product p1 ON fprm.fp_id = p1.product_id
INNER JOIN erp_mini.uom u ON p.base_uom_id = u.uom_id
INNER JOIN erp_mini.uom u1 ON p1.base_uom_id = u1.uom_id
ORDER BY p1.product_name;
Error Details:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=com.itsys.erp.server.dal.entities.Product.uom,tableName=Uom,tableAlias=uom4_,origin=Product product3_,columns={product3_.base_uom_id ,className=com.itsys.erp.server.dal.entities.Uom}}] [SELECT fprm FROM com.itsys.erp.server.dal.entities.FpRawMaterial fprm JOIN FETCH fprm.rawMaterial JOIN FETCH fprm.finishedProduct JOIN FETCH fprm.finishedProduct.uom ORDER BY fprm.finishedProduct.productName DESC]
In some others posts I read that the solution to remove the JOIN FETCH
and use just JOIN
, but that certainly creates N+1 issue.
Any ideas how it could be resolved?
Thanks.