Search code examples
hibernatejpaprojectiondto

exception when running jpa query with DTO and join fetch


I am trying to run a query with join fetch but I am also using DTO projection to improve performance but I am getting the exception below:

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=pi,role=returnitRest.Ereturn.productItems,tableName=product_item,tableAlias=productite1_,origin=ereturn ereturn0_,columns={ereturn0_.id ,className=returnitRest.ProductItem}}] [SELECT DISTINCT new returnitRest.Ereturn(e.rma, e.shipper, e.carrier, e.returnAction) FROM returnitRest.Ereturn e JOIN FETCH e.productItems pi WHERE e.status = 'RECEIVED' AND e.shipper.email = :shipper AND e.carrier.email = :carrier AND pi.returnAction = :returnAction ]

This is the query:

    em.createQuery("SELECT DISTINCT new returnitRest.Ereturn(e.rma, e.shipper, e.carrier, e.returnAction) FROM ereturn e " +
            "JOIN FETCH e.productItems pi " +
            "WHERE e.status = 'RECEIVED' AND " +
            "e.shipper.email = :shipper AND " +
            "e.carrier.email = :carrier AND " +
            "pi.returnAction = :returnAction ")
            .setParameter("shipper", shipperEmail)
            .setParameter("carrier", issuer)
            .setParameter("returnAction", ReturnAction.valueOf(returnAction))
            .getResultList();

QUESTIONS:

  • Who is the missing owner of the association?
  • What can I do to fix this query and still use DTO for performance gain and join fetch?

thank you very much


Solution

  • I could not make JOIN to work on a @OneToMany relationship so instead I did the otherway around which is using the @ManyToOne relationship. This way worked for me.