Q:

Hibernate lazy loading in new thread

I have a Spring application that is required to do a heavy data load on user login. So that the user doesn't have to wait for the data to load, I want to load this asynchronously. Unfortunately, I am dependent on some lazily loaded objects to load this data, which is causing problems with Hibernate, since the new thread has no session.
While I could load the objects in the main thread and pass them along after all the fields have been loaded, I'd rather avoid such a verbose approach. Is there a way to safely load these lazy entities in the new thread? Or potentially, is there a way for Spring to return a response to the client without having to close the current thread?

A:

I can not say whether this approach is the optimal solution, but by using Spring's @Transactional-annotation on all the methods used to reach the lazily loaded object in the new thread, in addition to the method that created the new thread, I was able to make this work without any additional code.
Of course, loading the object eagerly was also an option. Since I was using the JPA implementation in Spring, I simply could change my .getOne() call to a .findById() call.

