10

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.)

3
  • Please give us some details; enable the showSql option and look at the queries issued by hibernate and see if the lazy fields are retrieved during the transaction. Commented Oct 22, 2017 at 17:54
  • are you sure that the lazy objects really are initialized? Commented Oct 22, 2017 at 18:13
  • 1
    Lazy-loading has got nothing to do with transactions but the lifecycle of the EntityManager which is kept open by default for the entire request (see the answer by Babtiste below). Can you elaborate on the "hides bugs" statement? Why is it a bug to be able to call a method that returns a proper result? Commented Oct 23, 2017 at 14:00

1 Answer 1

14

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.