@@ -163,6 +163,7 @@ protected boolean readSize() {
163163 session .getPersistenceContextInternal ().getCollectionEntry ( this );
164164 if ( entry != null ) {
165165 final CollectionPersister persister = entry .getLoadedPersister ();
166+ checkPersister ( this , persister );
166167 if ( persister .isExtraLazy () ) {
167168 // TODO: support for extra-lazy collections was
168169 // dropped so this code should be obsolete
@@ -331,6 +332,7 @@ protected Boolean readIndexExistence(final Object index) {
331332 () -> {
332333 final CollectionEntry entry = session .getPersistenceContextInternal ().getCollectionEntry ( this );
333334 final CollectionPersister persister = entry .getLoadedPersister ();
335+ checkPersister ( this , persister );
334336 if ( persister .isExtraLazy () ) {
335337 if ( hasQueuedOperations () ) {
336338 session .flush ();
@@ -353,6 +355,7 @@ protected Boolean readElementExistence(final Object element) {
353355 () -> {
354356 final CollectionEntry entry = session .getPersistenceContextInternal ().getCollectionEntry ( this );
355357 final CollectionPersister persister = entry .getLoadedPersister ();
358+ checkPersister ( this , persister );
356359 if ( persister .isExtraLazy () ) {
357360 if ( hasQueuedOperations () ) {
358361 session .flush ();
@@ -399,6 +402,7 @@ public Object doWork() {
399402 session .getPersistenceContextInternal ()
400403 .getCollectionEntry ( AbstractPersistentCollection .this );
401404 final CollectionPersister persister = entry .getLoadedPersister ();
405+ checkPersister ( AbstractPersistentCollection .this , persister );
402406 isExtraLazy = persister .isExtraLazy ();
403407 if ( isExtraLazy ) {
404408 if ( hasQueuedOperations () ) {
@@ -646,12 +650,22 @@ private void throwLazyInitializationExceptionIfNotConnected() {
646650 }
647651
648652 private void throwLazyInitializationException (String message ) {
653+ throwLazyInitializationException ( role , message );
654+ }
655+
656+ private static void throwLazyInitializationException (String role , String message ) {
649657 throw new LazyInitializationException (
650658 String .format ( "Cannot lazily initialize collection%s (%s)" ,
651659 role == null ? "" : " of role '" + role + "'" , message )
652660 );
653661 }
654662
663+ public static void checkPersister (PersistentCollection collection , CollectionPersister persister ) {
664+ if ( !collection .wasInitialized () && persister == null ) {
665+ throwLazyInitializationException ( null , "collection is being removed" );
666+ }
667+ }
668+
655669 protected final void setInitialized () {
656670 this .initializing = false ;
657671 this .initialized = true ;
0 commit comments