<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/spring-boot-build-url-shortener-application---part-4-solving-jpa-hibernate-lazyloadingexception-hb6rbg5ml" -->

---
title: Spring Boot: Build URL Shortener Application - Part 4:...
description: A hands-on walkthrough of a Spring Boot URL shortener tutorial series tackles Hibernate/JPA's LazyInitializationException, explaining the open-session-in-view...
canonical: https://daily.dev/posts/spring-boot-build-url-shortener-application---part-4-solving-jpa-hibernate-lazyloadingexception-hb6rbg5ml
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Spring Boot: Build URL Shortener Application - Part 4: Solving JPA/Hibernate LazyLoadingException | daily.dev
og:description: A hands-on walkthrough of a Spring Boot URL shortener tutorial series tackles Hibernate/JPA's LazyInitializationException, explaining the open-session-in-view...
og:url: https://daily.dev/posts/spring-boot-build-url-shortener-application---part-4-solving-jpa-hibernate-lazyloadingexception-hb6rbg5ml
og:image: https://api.daily.dev/og/posts/hb6Rbg5mL.png
og:image:alt: Spring Boot: Build URL Shortener Application - Part 4: Solving JPA/Hibernate LazyLoadingException
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.

# Spring Boot: Build URL Shortener Application - Part 4: Solving JPA/Hibernate LazyLoadingException

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

## Summary

A hands-on walkthrough of a Spring Boot URL shortener tutorial series tackles Hibernate/JPA's LazyInitializationException, explaining the open-session-in-view setting, the N+1 select problem, and multiple fixes: JPQL left join fetch, entity graphs, and mapping entities to DTOs in the service layer. The video demonstrates disabling spring.jpa.open-in-view, restructuring repository queries, and using IntelliJ's DTO generation feature to avoid lazy loading exceptions in production.

## Full article

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

## Questions this post answers

### How do I fix Hibernate LazyInitializationException in Spring Boot when open-in-view is disabled?

Use a JPQL query with left join fetch to eagerly load the association in a single query, for example selecting a short URL entity with left join fetch on the created-by user field, instead of relying on lazy loading or the deprecated open-in-view session. Alternatively, use an @EntityGraph annotation specifying the attribute path to fetch, or map the entity into a DTO in the service layer so consumers never touch unloaded associations.

_daily.dev surfaces practical Hibernate fixes like this for developers debugging lazy loading errors._

### Why should spring.jpa.open-in-view be set to false in production?

Because the default value of true silently opens a database session during view rendering to lazily fetch associations, masking N+1 select problems that only surface as severe performance issues once production tables hold millions of records. Setting it to false forces developers to materialize all needed data explicitly in queries or DTOs, surfacing lazy loading exceptions early during development instead of causing slowdowns later.

_Track JPA configuration pitfalls like this on daily.dev before they hit production performance._

### What is the N+1 select problem in JPA and Hibernate?

It's a performance issue where fetching N parent rows, such as blog posts, triggers one query, and then lazily loading an associated collection for each row, like comments per post, fires N additional queries instead of one combined query. This becomes a serious bottleneck at scale; fixes include join fetch queries, entity graphs, or returning materialized DTOs from the service layer.

_daily.dev helps developers researching JPA performance issues like N+1 queries find practical fixes._

## Similar posts on daily.dev

- [Meet Jos Roseboom](https://daily.dev/posts/meet-jos-roseboom-slleuwote) · Vlad Mihalcea · 3 upvotes · 1 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/spring-boot-build-url-shortener-application---part-4-solving-jpa-hibernate-lazyloadingexception-hb6rbg5ml)

```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":"Spring Boot: Build URL Shortener Application - Part 4: Solving JPA/Hibernate LazyLoadingException","url":"https://daily.dev/posts/spring-boot-build-url-shortener-application---part-4-solving-jpa-hibernate-lazyloadingexception-hb6rbg5ml","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/spring-boot-build-url-shortener-application---part-4-solving-jpa-hibernate-lazyloadingexception-hb6rbg5ml"},"datePublished":"2026-09-27T07:26:43.325Z","dateModified":"2026-09-27T07:53:37.548Z","description":"A hands-on walkthrough of a Spring Boot URL shortener tutorial series tackles Hibernate/JPA's LazyInitializationException, explaining the open-session-in-view...","image":"https://i.ytimg.com/vi/3zSGp2BkAk0/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/3zSGp2BkAk0/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/spring-boot-build-url-shortener-application---part-4-solving-jpa-hibernate-lazyloadingexception-hb6rbg5ml","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java,spring","timeRequired":"PT21M","video":{"@type":"VideoObject","name":"Spring Boot: Build URL Shortener Application - Part 4: Solving JPA/Hibernate LazyLoadingException","description":"A hands-on walkthrough of a Spring Boot URL shortener tutorial series tackles Hibernate/JPA's LazyInitializationException, explaining the open-session-in-view...","thumbnailUrl":"https://i.ytimg.com/vi/3zSGp2BkAk0/sddefault.jpg","uploadDate":"2026-09-27T07:26:43.325Z","duration":"PT21M","url":"https://api.daily.dev/r/hb6Rbg5mL","embedUrl":"https://www.youtube.com/embed/3zSGp2BkAk0"}}
{"@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":"Spring Boot: Build URL Shortener Application - Part 4: Solving JPA/Hibernate LazyLoadingException"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/spring-boot-build-url-shortener-application---part-4-solving-jpa-hibernate-lazyloadingexception-hb6rbg5ml#faq","mainEntity":[{"@type":"Question","name":"How do I fix Hibernate LazyInitializationException in Spring Boot when open-in-view is disabled?","acceptedAnswer":{"@type":"Answer","text":"Use a JPQL query with left join fetch to eagerly load the association in a single query, for example selecting a short URL entity with left join fetch on the created-by user field, instead of relying on lazy loading or the deprecated open-in-view session. Alternatively, use an @EntityGraph annotation specifying the attribute path to fetch, or map the entity into a DTO in the service layer so consumers never touch unloaded associations. daily.dev surfaces practical Hibernate fixes like this for developers debugging lazy loading errors."}},{"@type":"Question","name":"Why should spring.jpa.open-in-view be set to false in production?","acceptedAnswer":{"@type":"Answer","text":"Because the default value of true silently opens a database session during view rendering to lazily fetch associations, masking N+1 select problems that only surface as severe performance issues once production tables hold millions of records. Setting it to false forces developers to materialize all needed data explicitly in queries or DTOs, surfacing lazy loading exceptions early during development instead of causing slowdowns later. Track JPA configuration pitfalls like this on daily.dev before they hit production performance."}},{"@type":"Question","name":"What is the N+1 select problem in JPA and Hibernate?","acceptedAnswer":{"@type":"Answer","text":"It's a performance issue where fetching N parent rows, such as blog posts, triggers one query, and then lazily loading an associated collection for each row, like comments per post, fires N additional queries instead of one combined query. This becomes a serious bottleneck at scale; fixes include join fetch queries, entity graphs, or returning materialized DTOs from the service layer. daily.dev helps developers researching JPA performance issues like N+1 queries find practical fixes."}}]}
```

