Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why the lazy collection is loaded

I have a Project entity with a oneToMany relationship with Event entity

public class Project {
   ....

   @OneToMany(mappedBy = "dossier", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
   private List<Event> events;

}

I have a ProjectService class

@Service
@Transactional
public class ProjectService {

    public List<Project> findAll() {
        return (List<Project>) projectRepository.findAll();
    }
}

And a ProjectController

@RestController
@RequestMapping(value = "/projects")
public class ProjectController {

    @RequestMapping(method= RequestMethod.GET)
    public List<Project> getAllProject() {
        return projectService.findAll();
    }
}

In my client code I saw that the events of the projects are loaded and I don't understand why.

I expected that at the end of the transaction of the method findAll in DossierService, the entities will be detached. Obviously my entities are still attached as the events are retrieved during the jackson serialization in my controller.

like image 916
Olivier Boissé Avatar asked Jan 06 '23 12:01

Olivier Boissé


1 Answers

Project.events is by default lazy-loaded because it is a OneToMany relationship.

It does not mean that Project.events is not loaded. It means that it will be loaded as soon as Project.getEvents() is called.

This occurs at the JSON serialization (when ProjectController.getAllProject() returns its response).

In order to prevent that, there are 2 ways :

  • Either you explicitly call project.setEvents(null) (or an empty list) on every project returned by ProjectService.
  • Or you add a @JsonIgnore annotation on Project.events.

EDIT: if you are using spring-boot, an OpenEntityManagerInViewInterceptor is registered by default :

Spring web request interceptor that binds a JPA EntityManager to the thread for the entire processing of the request. Intended for the "Open EntityManager in View" pattern, i.e. to allow for lazy loading in web views despite the original transactions already being completed.

You can disable this behavior by adding this line to application.properties :

spring.jpa.open-in-view=false

With this configuration, calling the getter outside of the hibernate session will cause a LazyInitializationException.

like image 195
Arnaud Denoyelle Avatar answered Jan 12 '23 22:01

Arnaud Denoyelle