The best way to use JOIN FETCH and Pagination with Spring
Introduction In this article, we are going to see how we can use the JOIN FETCH clause when fetching a child collection eagerly while also limiting the number of parent records using pagination in a Spring Data JPA application. I decided to write this article because the most common solution used in many projects turns out to be extremely inefficient.
JPA Association Fetching Validator
Introduction In this article, I’m going to show you how we can build a JPA Association Fetching Validator that asserts whether JPA and Hibernate associations are fetched using joins or secondary queries. While Hibernate does not provide built-in support for checking the entity association fetching behavior programmatically, the API is very flexible and allows us to customize it so that we can achieve this non-trivial requirement.
The best way to fix the Hibernate MultipleBagFetchException
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.
Why you should avoid EXTRA Lazy Collections with Hibernate
Introduction In this article, you are going to learn why using EXTRA Lazy Collections with Hibernate is a bad idea since it can lead to N+1 query issues and cause performance problems. The reason I wanted to write this article is that I keep seeing it mentioned in StackOverflow or the Hibernate forum.
The best way to fix the Hibernate “firstResult/maxResults specified with collection fetch; applying in memory!” warning message
Introduction If you’ve been using Hibernate long enough, then you surely must have seen this WARN log message when doing pagination while join-fetching multiple entities. HHH000104: firstResult/maxResults specified with collection fetch; applying in memory! In this article, I’m going to show two ways you can to fix this issue.
How to lazy load entity properties with Hibernate
Introduction One of my readers bumped into the JSON mapping post and asked me if we can fetch the JSON properties lazily. This post demonstrates how easily this can be done when using Hibernate as a JPA provider. As I previously explained, EAGER fetching is a code smell and loading associations eagerly is very detriment to application performance. However, it’s not just associations that we must be careful about. Basic entity properties may also cause performance issues as well. In this post, I’m going to show you how you can fetch entity… Read More
The Open Session In View Anti-Pattern
Introduction Open Session in View is an Anti-Pattern, and this post is going to demonstrate why it is so. First of all, let’s start with the Wikipedia definition of an Anti-Pattern: An anti-pattern (or antipattern) is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive.
High-Performance Java Persistence – Chapter 15 – Fetching
Part 2, Chapter 15 Every new chapter of my book is released right after it’s being completed, so the reader doesn’t have to wait for the whole part to be finished to get access to new material. Table of content This chapter explains how fetching works in Hibernate.