Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate is not throwing LazyInitializationException in Spring Boot Project

I work on a Spring Boot project with Hibernate 5.0. Unfortunately Hibernate reads the lazy initialized objects without throwing LazyInitializationException even after the transaction is committed. How can I enable the LazyInitializationException outside of transactions?

(The current behavior hides bugs in the code.)

like image 330
user1552545 Avatar asked Oct 22 '17 17:10

user1552545


1 Answers

Spring boot have a property spring.jpa.open-in-view with a default value of true. This will register a OpenEntityManagerInViewInterceptor that will keep the transaction alive for the entire request.

try adding this to your application.properties file:

spring.jpa.open-in-view=false

A discussion about it on GitHub

Some documentation about Spring Boot properties

Hope it helped!

like image 67
Baptiste Beauvais Avatar answered Oct 15 '22 21:10

Baptiste Beauvais