The best way to fix the Hibernate MultipleBagFetchException

Imagine having a tool that can automatically detect JPA and Hibernate performance issues. Hypersistence Optimizer is that tool!

Introduction

If you’ve been using Hibernate for some time, there is a good chance you bumped into a MultipleBagFetchException issue:

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

In this article, we are going to see the reason Hibernate throws the MultipleBagFetchException as well as the best way to solve this issue.

Domain Model

Let’s consider that our application defines three entities: Post, PostComment, and Tag, which are associated as in the following diagram:

Multiple List Eager Fetching

What we are mostly interested in this article is that the Post entity defines a bidirectional @OneToMany association with the PostComment child entity, as well as a unidirectional @ManyToMany association with the Tag entity.

@OneToMany(
    mappedBy = "post", 
    cascade = CascadeType.ALL, 
    orphanRemoval = true
)
private List<PostComment> comments = new ArrayList<>();

@ManyToMany(
    cascade = {
        CascadeType.PERSIST, 
        CascadeType.MERGE
    }
)
@JoinTable(
    name = "post_tag",
    joinColumns = @JoinColumn(name = "post_id"),
    inverseJoinColumns = @JoinColumn(name = "tag_id")
)
private List<Tag> tags = new ArrayList<>();

The reason why the @ManyToMany association cascades only the PERSIST and MERGE entity state transitions and not the REMOVE one is because the other side is not a child entity.

Since the Tag entity lifecycle is not tied to the Post entity, cascading REMOVE or enabling the orphanRemoval mechanism would be a mistake. For more details about this topic, check out this article.

Hibernate throwing MultipleBagFetchException

Now, if we want to fetch the Post entities with the identifier values between 1 and 50, along with all their associated PostComment and Tag entities, we would write a query like the following one:

List<Post> 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();

However, when running the entity query above, Hibernate throws a MultipleBagFetchException while compiling the JPQL query:

org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags [
  com.vladmihalcea.book.hpjp.hibernate.fetching.Post.comments, 
  com.vladmihalcea.book.hpjp.hibernate.fetching.Post.tags
]

So, no SQL query is executed by Hibernate. The reason why a MultipleBagFetchException is thrown by Hibernate is that duplicates can occur, and the unordered List, which is called a bag in Hibernate terminology, is not supposed to remove duplicates.

How NOT to “fix” the Hibernate MultipleBagFetchException

If you google the MultipleBagFetchException, you are going to see many wrong answers, like this one on StackOverflow, which, surprisingly, has over 280 upvotes.

StackOverflow MultipleBagFetchException Wrong Answer

So simple, yet so wrong!

Using Set instead of List

So, let’s change the association collection type from List to Set:

@OneToMany(
    mappedBy = "post", 
    cascade = CascadeType.ALL, 
    orphanRemoval = true
)
private Set<PostComment> comments = new HashSet<>();

@ManyToMany(
    cascade = {
        CascadeType.PERSIST, 
        CascadeType.MERGE
    }
)
@JoinTable(
    name = "post_tag",
    joinColumns = @JoinColumn(name = "post_id"),
    inverseJoinColumns = @JoinColumn(name = "tag_id")
)
private Set<Tag> tags = new HashSet<>();

And, now, when rerunning the previous entity query which fetched some Post entities along with their comments and tags associations, we can see that no MultipleBagFetchException is thrown.

However, this is SQL query that Hibernate executed for the aforementioned JPQL query:

SELECT 
    p.id AS id1_0_0_,
    pc.id AS id1_1_1_,
    t.id AS id1_3_2_,
    p.title AS title2_0_0_,
    pc.post_id AS post_id3_1_1_,
    pc.review AS review2_1_1_,
    t.name AS name2_3_2_,
    pt.post_id AS post_id1_2_1__,
    pt.tag_id AS tag_id2_2_1__
FROM 
    post p
LEFT OUTER JOIN 
    post_comment pc ON p.id = pc.post_id
LEFT OUTER JOIN 
    post_tag pt ON p.id = pt.post_id
LEFT OUTER JOIN 
    tag t ON pt.tag_id = t.id
WHERE 
    p.id BETWEEN 1 AND 50

So, what’s wrong with this SQL query?

The post and post_comment are associated via the post_id Foreign Key column, so the join produces a result set containing all post table rows with the Primary Key values between 1 and 50 along with their associated post_comment table rows.

The post and tag tables are also associated via the post_id and tag_id post_tag Foreign Key columns, so these two joins produce a result set containing all post table rows with the Primary Key values between 1 and 50 along with their associated tag table rows.

Now, to merge the two result sets, the database can only use a Cartesian Product, so the final result set contains 50 post rows multiplied by the associated post_comment and tag table rows.

So, if we have 50 post rows associated with 20 post_comment and 10 tag rows, the final result set will contain 10_000 records (e.g., 50 x 20 x 10), as illustrated by the following test case:

List<Post> 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();

assertEquals(
    POST_COUNT * POST_COMMENT_COUNT * TAG_COUNT, 
    posts.size()
);

That’s so terrible from a performance perspective!

How to fix the Hibernate MultipleBagFetchException

To avoid a Cartesian Product, you can fetch at most one association at a time. So, instead of executing a single JPQL query that fetches two associations, we can execute two JPQL queries instead:

List<Post> 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();

assertEquals(POST_COUNT, posts.size());

for(Post post : posts) {
    assertEquals(POST_COMMENT_COUNT, post.getComments().size());
    assertEquals(TAG_COUNT, post.getTags().size());
}

The first JPQL query defines the main filtering criteria and fetches the Post entities along with the associated PostComment records.

The PASS_DISTINCT_THROUGH query hint allows you to avoid passing the DISTINCT keyword to the SQL statement, and only use it to remove Java entity duplicates caused by the parent-child joined result set. For more details about the PASS_DISTINCT_THROUGH query hint, check out this article.

Now, we have to fetch the Post entities along with their associated Tag entities, and, thanks to the Persistence Context, Hibernate will set the tags collection of the previously fetched Post entities.

Cool, right?

I'm running an online workshop on the 27th of May about High-Performance SQL.

If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.

Conclusion

There are so many blog posts, videos, books, and forum answers, providing the wrong solution to the MultipleBagFetchException Hibernate issues. All these resources tell you that using a Set instead of a List is the right way to avoid this exception.

However, the MultipleBagFetchException tells you that a Cartesian Product might be generated, and, most of the time, that’s undesirable when fetching entities as it can lead to terrible data access performance issues.

The best way to fetch multiple entity collections with JPA and Hibernate is to load at most one collection at a time while relying on the Hibernate Persistence Context guarantee that only a single entity object can be loading at a time in a given JPA EntityManager or Hibernate Session.

Transactions and Concurrency Control eBook

10 Comments on “The best way to fix the Hibernate MultipleBagFetchException

  1. Hi Vlad, recently you made a very interesting comment on StackOverflow regarding a specific use-case and I thought it would be interesting to be posted here:

    “If you fetch two sibling Set associations using any strategy, you’ll get a Cartesian Product. If the associations are related, like child and grandchildren, then you won’t get a Cartesian Product.”

    Does that mean that if you want a list of all the comments on a certain tag you can just do:

    @EntityGraph(attributePaths = {“tag.posts.comments”})

    whithout getting the Cartesian Product problem?

  2. Hi Vlad,

    Thanks for this article ! I just update from hibernate 4 to 5 and now, when the application is starting and when Configuration#buildSessionFactory is called, I’ve got this expcetion thrown even if i didn’t create any query. Do you ever seen this case ?

    PS : We have QueryDSL in the project too

  3. Thanks Vlad, very helpful!

    Would you mind showing how to do this with the Criteria API? All the methods I see make you set a property name rather than setting the main Criteria list.

  4. Hi Vlad,

    thanks for the great article. Confronted with this exception for the first time, I ran into a suggestion to use @Fetch(value = FetchMode.SUBSELECT) at one of the collections. Now for 2-3 months working with this approach, I have not faced any problems, admitably in a stage where the application is not so complex, but what is your expereince on using this approach? Are there any pitfalls when using the @Fetch Annotation as a solution for fetching multiple bags?

    I really appreciate your opinion.

    Thanks in advance!

    • The FetchMode.SUBSELECT can also be a solution to this problem, but accessing the properties just to trigger the extra SELECT is not ideal and the JIT might even remove the call if you don’t use the returning value.

      • Thanks Vlad, this is important information to have in mind!

  5. if i use FethType.Lazy the what will happen..? please tell me

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.