org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

前端 未结 7 836
南笙
南笙 2020-12-04 20:07

Following is my code Here I am using multiple lists to fetch data from database. On fetching data from hql query it is showing exception.

Pojo Class

         


        
7条回答
  •  既然无缘
    2020-12-04 20:45

    This is a very common question, so I decided to turn the answer into an article.

    Hibernate doesn't allow fetching more than one bag because that would generate a Cartesian product.

    Now, you will find lots of answers, blog posts, videos, or other resources telling you to use a Set instead of a List for your collections.

    That's terrible advice!

    Using Sets instead of Lists will make the MultipleBagFetchException go away, but the Cartesian Product will still be there.

    The right fix

    Instead of using multiple JOIN FETCH in a single JPQL or Criteria API query:

    List posts = entityManager.createQuery("""
        select p
        from Post p
        left join fetch p.comments
        left join fetch p.tags
        where p.id between :minId and :maxId
        """, Post.class)
    .setParameter("minId", 1L)
    .setParameter("maxId", 50L)
    .getResultList();
    

    You can do the following trick:

    List posts = entityManager.createQuery("""
        select distinct p
        from Post p
        left join fetch p.comments
        where p.id between :minId and :maxId
        """, Post.class)
    .setParameter("minId", 1L)
    .setParameter("maxId", 50L)
    .setHint(QueryHints.PASS_DISTINCT_THROUGH, false)
    .getResultList();
    
    posts = entityManager.createQuery("""
        select distinct p
        from Post p
        left join fetch p.tags t
        where p in :posts
        """, Post.class)
    .setParameter("posts", posts)
    .setHint(QueryHints.PASS_DISTINCT_THROUGH, false)
    .getResultList();
    

    As long as you fetch at most one collection using JOIN FETCH, you will be fine. By using multiple queries, you will avoid the Cartesian Product since any other collection but the first one is fetched using a secondary query.

提交回复
热议问题