Hibernate Turned 11,000 Records Into 50 Million Rows
This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).
A bulk-import job processing 15,000 transaction templates caused an out-of-memory crash due to a Hibernate Cartesian product bug. A single JPQL query using JOIN FETCH on two collections simultaneously caused Hibernate to cross-join them, turning ~11,000 records into 50 million rows in memory. The bug was invisible in small-scale tests because the collections were mapped as Set rather than List (which would have thrown a MultipleBagFetchException at startup). The fix involved splitting the query into two separate fetches per collection, using @BatchSize for batch loading, and periodically flushing and clearing the Hibernate session during long-running imports. The result was a 9x reduction in row count and 15x improvement in processing time.
Table of contents
The Query That Looked Totally FineQuick Refresher: Eager vs Lazy LoadingHere’s What’s Actually HappeningGet JAGRITI BANSAL ’s stories in your inboxWhy Nobody Caught It in ReviewHow We Tracked It DownThe Fix, in Three PartsDid It Actually WorkWhat I’d Tell You to Watch For91K Impressions1 Comment