'JPQL Join Fetch in jpql not working, running into org.springframework.dao.InvalidDataAccessApiUsageException

I am using JPQL queries for my rest application. spring boot 2.5 and mysql 5.7

I have one table that has 4 onetomany and I need to fetch all the relationships at one go for the findAll query.

In order to achieve that, I am using JPQL query with join fetch. However I am not able to load data using join fetch for this.

@Repository
@Transactional
public interface CustomProductRepository extends JpaRepository<Product, Long>, JpaSpecificationExecutor<Product> {

    default Page<Product> findByPagingCriteria(List<Long> productIds, Long categoryId, Long brandId, String partNumber, Pageable pageable) {

        Page page = this.findAll((Specification<Product>) (root, query, criteriaBuilder) -> {

            //setting fetch to load data at once instead of firing multiple queries
            root.fetch("productModelMappings", JoinType.LEFT);
            root.fetch("offers", JoinType.LEFT);
            root.fetch("productAttributes", JoinType.LEFT);
            root.fetch("productSellers", JoinType.LEFT);

            List<Predicate> predicates = new ArrayList<>();

//            query.where(root.get("id").in(Arrays.asList(1)));

            if (categoryId != null) {
                predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("categoryId"), categoryId)));
            }
            if (brandId != null) {
                predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("brandId"), brandId)));
            }
            if (StringUtils.isNotEmpty(partNumber)) {
                predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("partNumber"), partNumber)));
            }

            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        }, pageable);
        page.getTotalElements();        // get total elements
        page.getTotalPages();           // get total pages
        return page;
    }
}

Getting the below error:

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=generatedAlias1,role=com.pitstop.catalogue.domain.Product.productModelMappings,tableName=product_model_mapping,tableAlias=productmod1_,origin=product product0_,columns={product0_.id,className=com.pitstop.catalogue.domain.ProductModelMapping}}] [select count(generatedAlias0) from com.pitstop.catalogue.domain.Product as generatedAlias0 left join fetch generatedAlias0.productModelMappings as generatedAlias1 left join fetch generatedAlias0.offers as generatedAlias2 left join fetch generatedAlias0.productAttributes as generatedAlias3 left join fetch generatedAlias0.productSellers as generatedAlias4 where 1=1]; nested exception is java.lang.IllegalArgumentException: 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=generatedAlias1,role=com.pitstop.catalogue.domain.Product.productModelMappings,tableName=product_model_mapping,tableAlias=productmod1_,origin=product product0_,columns={product0_.id,className=com.pitstop.catalogue.domain.ProductModelMapping}}] [select count(generatedAlias0) from com.pitstop.catalogue.domain.Product as generatedAlias0 left join fetch generatedAlias0.productModelMappings as generatedAlias1 left join fetch generatedAlias0.offers as generatedAlias2 left join fetch generatedAlias0.productAttributes as generatedAlias3 left join fetch generatedAlias0.productSellers as generatedAlias4 where 1=1]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source