mailwal

The right kind of lazy ...

Thorben

Hey,

I thought about starting this email with a joke about being as lazy as my entity associations.

But let's be honest

... nobody wants to read a lame joke.

So, let's better jump directly to the main topic and talk about the LazyInitializationException. It's one of the most common exceptions and clearly shows that something has gone wrong.

Hibernate throws it when you fetched an entity with a lazily fetched association and access that association outside of the transactional context that fetched the entity.

As I showed in this article, you have multiple options to fix this problem.

Unfortunately, there are also a few "fixes" you should better avoid. One of them is the open session in view anti-pattern used by Spring Boot. Another one is changing the FetchType to EAGER. (Yes, this is still suggested in some discussions on StackOverflow and other places.)

Speak soon,

Thorben

  1. Check out the latest tutorials on the blog.
    Stay up to date with the latest releases and features in the Java persistence world. Read the blog.

  2. Watch my tutorials on YouTube.
    You prefer videos over written content? Then my YouTube channel is the right place for you.

  3. Get my Hibernate Tips ebook.
    The ebook is packed with ready-to-use solutions to help you solve your next Hibernate problem even quicker. Get it here.

  4. Join the Persistence Hub.
    The Persistence Hub teaches you everything you need to know on your journey to becoming a Java persistence expert. Get access to video courses, Expert Sessions with amazing guests, monthly Java Persistence News, and more. Become a member.

Thorben Janssen, Elser Bruch 63, 33106, Paderborn, Germany