Class MediaLazyLoader

java.lang.Object
com.espacogeek.geek.utils.MediaLazyLoader

@Component public class MediaLazyLoader extends Object
Utility Spring bean that guarantees all lazy-loaded Set<> collections of a MediaModel are available for use outside an active Hibernate session.

With Java 21 virtual threads, thread-local Hibernate sessions can be closed before update methods finish processing a detached entity, causing LazyInitializationException. This class solves the problem once and for all by checking Hibernate.isInitialized(java.lang.Object) for every lazy collection and falling back to a fresh repository query when the session is no longer active.

Covered collections: externalReference, alternativeTitles, genre, season.

  • Constructor Details

  • Method Details

    • initializeCollections

      public void initializeCollections(MediaModel media)
      Ensures all lazy Set collections of the given MediaModel are initialized. For each collection that is null or not yet initialized by Hibernate (e.g., because the session that loaded the entity has been closed), the data is loaded directly from the corresponding service and set back on the entity.
      Parameters:
      media - the MediaModel to initialize; no-op when null or unsaved