WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Boot & Hibernate · Intermediate · question 30 of 100

What is the N+1 problem in Hibernate, and how can you solve or mitigate it in a Spring Boot application?

📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

The N+1 problem is a common performance issue that can occur when using Hibernate to access a database in a Spring Boot application. The problem arises when Hibernate generates multiple SQL queries to retrieve related entities, instead of retrieving all the necessary data in a single query. This can result in an excessive number of SQL queries being executed, leading to poor application performance.

Here is an example of the N+1 problem:

    @Entity
    public class Order {
        @Id
        @GeneratedValue
        private Long id;
        
        @OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
        private List<OrderItem> items;
        
        // getters and setters
    }
    
    @Entity
    public class OrderItem {
        @Id
        @GeneratedValue
        private Long id;
        
        @ManyToOne(fetch = FetchType.LAZY)
        private Order order;
        
        // getters and setters
    }

In this example, we have two entities, Order and OrderItem, with a one-to-many relationship between them. When we retrieve all orders, Hibernate will generate a separate SQL query for each order item, resulting in N+1 queries.

To solve or mitigate the N+1 problem, there are several approaches that can be taken:

Eager fetching: One approach to solving the N+1 problem is to use eager fetching instead of lazy fetching for related entities. This will ensure that all related entities are loaded in a single SQL query. However, eager fetching can result in unnecessary data being loaded, which can impact performance.

    @Entity
    public class Order {
        @Id
        @GeneratedValue
        private Long id;
        
        @OneToMany(mappedBy = "order", fetch = FetchType.EAGER)
        private List<OrderItem> items;
        
        // getters and setters
    }

Batch fetching: Another approach is to use batch fetching to retrieve related entities in batches. This can be done by setting the batch-size attribute on the @OneToMany or @ManyToOne annotation. Batch fetching will result in fewer SQL queries being executed, and can improve performance.

    @Entity
    public class Order {
        @Id
        @GeneratedValue
        private Long id;
        
        @OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
        @BatchSize(size = 10)
        private List<OrderItem> items;
        
        // getters and setters
    }
    
    @Entity
    public class OrderItem {
        @Id
        @GeneratedValue
        private Long id;
        
        @ManyToOne(fetch = FetchType.LAZY)
        @BatchSize(size = 10)
        private Order order;
        
        // getters and setters
    }

Fetch join: A fetch join can be used to retrieve related entities in a single SQL query. A fetch join is performed by adding the fetch keyword to a join clause in a JPQL query.

    @Repository
    public interface OrderRepository extends JpaRepository<Order, Long> {
        @Query("SELECT o FROM Order o JOIN FETCH o.items")
        List<Order> findAllWithItems();
    }

In this example, we have defined a custom query that uses a fetch join to retrieve all orders with their related order items in a single SQL query.

Overall, the N+1 problem is a common performance issue that can be mitigated using various techniques. By carefully tuning Hibernate’s fetching strategy and using fetch joins, batch fetching, or eager fetching, developers can ensure that their Spring Boot applications perform efficiently and scale well.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Boot & Hibernate interview — then scores it.
📞 Practice Spring Boot & Hibernate — free 15 min
📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

All 100 Spring Boot & Hibernate questions · All topics