Package com.espacogeek.geek.utils
Class MediaLazyLoader
java.lang.Object
com.espacogeek.geek.utils.MediaLazyLoader
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 Summary
ConstructorsConstructorDescriptionMediaLazyLoader(ExternalReferenceService externalReferenceService, AlternativeTitlesService alternativeTitlesService, GenreService genreService, SeasonService seasonService) -
Method Summary
Modifier and TypeMethodDescriptionvoidinitializeCollections(MediaModel media) Ensures all lazySetcollections of the givenMediaModelare initialized.
-
Constructor Details
-
MediaLazyLoader
public MediaLazyLoader(ExternalReferenceService externalReferenceService, AlternativeTitlesService alternativeTitlesService, GenreService genreService, SeasonService seasonService)
-
-
Method Details
-
initializeCollections
Ensures all lazySetcollections of the givenMediaModelare initialized. For each collection that isnullor 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- theMediaModelto initialize; no-op whennullor unsaved
-