Skip to content
Advertisement

JPA Lazy loading is not working in Spring boot

I googled a lot and It is really bizarre that Spring Boot (latest version) may not have the lazy loading is not working. Below are pieces of my code:

My resource:

JavaScript

My service:

JavaScript

My Entity:

JavaScript

And when debugging, I still getting all lazy loaded attributed loaded. See image below.

enter image description here

One of my questions is could Jackson be involved in such behaviour? Is there any way that I may have missed to activate the lazy loading?

EDIT

Another question, could the debugger be involved in ruining the lazy loading?

EDIT 2:

For specification build, I have :

JavaScript

Advertisement

Answer

Hibernate Session exists within method with @Transactional. Passing entity outside Service class is a bad practise because session is being closed after leaving your search method. On the other hand your entity contains lazy initialised collections, which cannot be pulled once session is closed.

The good practise is to map entity onto transport object and return those transport objects from service (not raw entities).

Advertisement