<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-fix-hibernate-lazyinitializationexception--mq6dug0qo" -->

---
title: How to Fix Hibernate LazyInitializationException?
description: A walkthrough explains why Hibernate's LazyInitializationException occurs in a Spring Boot app using Spring Data JPA, tracing it to the spring.jpa.open-in-view...
canonical: https://daily.dev/posts/how-to-fix-hibernate-lazyinitializationexception--mq6dug0qo
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to Fix Hibernate LazyInitializationException? | daily.dev
og:description: A walkthrough explains why Hibernate's LazyInitializationException occurs in a Spring Boot app using Spring Data JPA, tracing it to the spring.jpa.open-in-view...
og:url: https://daily.dev/posts/how-to-fix-hibernate-lazyinitializationexception--mq6dug0qo
og:image: https://api.daily.dev/og/posts/mq6DUg0Qo.png
og:image:alt: How to Fix Hibernate LazyInitializationException?
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Fix Hibernate LazyInitializationException?

**[SivaLabs \(YouTube\)](https://daily.dev/sources/sivalabs-youtube)** · 19 min read · 0 upvotes · 0 comments

## Summary

A walkthrough explains why Hibernate's LazyInitializationException occurs in a Spring Boot app using Spring Data JPA, tracing it to the spring.jpa.open-in-view setting and uninitialized lazy collections accessed outside a Hibernate session. It demonstrates two quick-but-flawed fixes (leaving open-in-view enabled, or switching associations to eager loading) that both cause N+1 select problems, then walks through three better solutions: JPQL queries with LEFT JOIN FETCH, @EntityGraph attribute paths, and Spring Data JPA projections. The presenter recommends projections for read-only use cases since they avoid loading full entities and sidestep the exception entirely.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=a0mzCZNEvLk>

## Questions this post answers

### What causes Hibernate's LazyInitializationException in a Spring Boot REST API?

It occurs when code tries to access an uninitialized lazy collection, like a post's comments, after the Hibernate session has already closed. This typically happens when spring.jpa.open-in-view is set to false and a controller maps a JPA entity to a DTO outside the transactional service method, triggering lazy loading with no active session available.

_Developers debugging this exact stack trace can find related Hibernate and JPA fixes curated on daily.dev._

### Why does turning off spring.jpa.open-in-view cause more errors than leaving it enabled?

Leaving open-in-view enabled (the Spring Boot default) keeps a Hibernate session open for the whole request, so lazy collections load silently later, but this causes N+1 select queries, firing one query per parent row to fetch child records like comments, which burdens the database. Turning it off exposes these issues as LazyInitializationExceptions instead of hiding them.

_Teams weighing open-in-view trade-offs can track more JPA performance guidance through daily.dev._

### How do I fix N+1 select queries when loading JPA entities with lazy collections?

Three approaches work: write a custom JPQL query using LEFT JOIN FETCH to load the parent and its collection in one SQL join, use @EntityGraph to declare which lazy attribute paths to eagerly fetch, or use Spring Data JPA projections to load only the needed fields without touching entity proxies at all. Projections are preferred for read-only endpoints since they avoid the persistence context entirely.

_Anyone optimizing read-heavy JPA endpoints can keep up with these patterns via daily.dev._

## Similar posts on daily.dev

- [How to emulate LEFT JOIN FETCH using projections](https://daily.dev/posts/how-to-emulate-left-join-fetch-using-projections-z5aul2lhn) · Vlad Mihalcea · 2 upvotes · 0 comments
- [Solving org.hibernate.AnnotationException: Illegal Attempt to Map a Non Collection](https://daily.dev/posts/solving-org-hibernate-annotationexception-illegal-attempt-to-map-a-non-collection-lgaldi4jb) · Baeldung · 1 upvotes · 0 comments

---

Tags: [#spring](https://daily.dev/tags/spring), [#spring-boot](https://daily.dev/tags/spring-boot)

[View this post on daily.dev](https://daily.dev/posts/how-to-fix-hibernate-lazyinitializationexception--mq6dug0qo)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to Fix Hibernate LazyInitializationException?","url":"https://daily.dev/posts/how-to-fix-hibernate-lazyinitializationexception--mq6dug0qo","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-fix-hibernate-lazyinitializationexception--mq6dug0qo"},"datePublished":"2026-09-27T07:26:42.990Z","dateModified":"2026-09-27T07:53:28.938Z","description":"A walkthrough explains why Hibernate's LazyInitializationException occurs in a Spring Boot app using Spring Data JPA, tracing it to the spring.jpa.open-in-view...","image":"https://i.ytimg.com/vi/a0mzCZNEvLk/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/a0mzCZNEvLk/sddefault.jpg","isAccessibleForFree":true,"articleSection":"SivaLabs (YouTube)","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"SivaLabs (YouTube)","logo":"https://media.daily.dev/image/upload/s--8ATVo4an--/f_auto,q_auto/v1790493987/logos/sivalabs-youtube?_a=BAMAMicg0","url":"https://daily.dev/sources/sivalabs-youtube"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-fix-hibernate-lazyinitializationexception--mq6dug0qo","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"spring,spring-boot","timeRequired":"PT19M","video":{"@type":"VideoObject","name":"How to Fix Hibernate LazyInitializationException?","description":"A walkthrough explains why Hibernate's LazyInitializationException occurs in a Spring Boot app using Spring Data JPA, tracing it to the spring.jpa.open-in-view...","thumbnailUrl":"https://i.ytimg.com/vi/a0mzCZNEvLk/sddefault.jpg","uploadDate":"2026-09-27T07:26:42.990Z","duration":"PT19M","url":"https://api.daily.dev/r/mq6DUg0Qo","embedUrl":"https://www.youtube.com/embed/a0mzCZNEvLk"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"SivaLabs (YouTube)","item":"https://daily.dev/sources/sivalabs-youtube"},{"@type":"ListItem","position":3,"name":"How to Fix Hibernate LazyInitializationException?"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-fix-hibernate-lazyinitializationexception--mq6dug0qo#faq","mainEntity":[{"@type":"Question","name":"What causes Hibernate's LazyInitializationException in a Spring Boot REST API?","acceptedAnswer":{"@type":"Answer","text":"It occurs when code tries to access an uninitialized lazy collection, like a post's comments, after the Hibernate session has already closed. This typically happens when spring.jpa.open-in-view is set to false and a controller maps a JPA entity to a DTO outside the transactional service method, triggering lazy loading with no active session available. Developers debugging this exact stack trace can find related Hibernate and JPA fixes curated on daily.dev."}},{"@type":"Question","name":"Why does turning off spring.jpa.open-in-view cause more errors than leaving it enabled?","acceptedAnswer":{"@type":"Answer","text":"Leaving open-in-view enabled (the Spring Boot default) keeps a Hibernate session open for the whole request, so lazy collections load silently later, but this causes N+1 select queries, firing one query per parent row to fetch child records like comments, which burdens the database. Turning it off exposes these issues as LazyInitializationExceptions instead of hiding them. Teams weighing open-in-view trade-offs can track more JPA performance guidance through daily.dev."}},{"@type":"Question","name":"How do I fix N+1 select queries when loading JPA entities with lazy collections?","acceptedAnswer":{"@type":"Answer","text":"Three approaches work: write a custom JPQL query using LEFT JOIN FETCH to load the parent and its collection in one SQL join, use @EntityGraph to declare which lazy attribute paths to eagerly fetch, or use Spring Data JPA projections to load only the needed fields without touching entity proxies at all. Projections are preferred for read-only endpoints since they avoid the persistence context entirely. Anyone optimizing read-heavy JPA endpoints can keep up with these patterns via daily.dev."}}]}
```

