JPA: please help understanding “join fetch”

后端 未结 4 648
太阳男子
太阳男子 2021-02-01 08:36

I have the following entity structure: Business --> Campaign --> Promotion, where ONE Business can have MANY Campaigns and ONE Campaign can have MANY Promotions. Both one-to-man

4条回答
  •  不知归路
    2021-02-01 09:08

    Another workaround instead of using Set is to use two queries to get the data :- 1st one to load the campaign and its associated promotions of the business. Then load the Business and its campaigns using fetch

        Query query1 = entityManager.createQuery("select c from Business b join b.campaigns c left join fetch c.promotions where b.id=:id");
        query1.setParameter("id", b.getId());
        query1.getResultList();
    
        Query query2 = entityManager.createQuery("select b from Business b left join fetch       b.campaigns where b.id=:id");
        query2.setParameter("id", b.getId());
        business = (Business) query2.getResultList().get(0);
    

提交回复
热议问题