@@ -163,6 +163,7 @@ protected boolean readSize() {
163
163
session .getPersistenceContextInternal ().getCollectionEntry ( this );
164
164
if ( entry != null ) {
165
165
final CollectionPersister persister = entry .getLoadedPersister ();
166
+ checkPersister ( this , persister );
166
167
if ( persister .isExtraLazy () ) {
167
168
// TODO: support for extra-lazy collections was
168
169
// dropped so this code should be obsolete
@@ -331,6 +332,7 @@ protected Boolean readIndexExistence(final Object index) {
331
332
() -> {
332
333
final CollectionEntry entry = session .getPersistenceContextInternal ().getCollectionEntry ( this );
333
334
final CollectionPersister persister = entry .getLoadedPersister ();
335
+ checkPersister ( this , persister );
334
336
if ( persister .isExtraLazy () ) {
335
337
if ( hasQueuedOperations () ) {
336
338
session .flush ();
@@ -353,6 +355,7 @@ protected Boolean readElementExistence(final Object element) {
353
355
() -> {
354
356
final CollectionEntry entry = session .getPersistenceContextInternal ().getCollectionEntry ( this );
355
357
final CollectionPersister persister = entry .getLoadedPersister ();
358
+ checkPersister ( this , persister );
356
359
if ( persister .isExtraLazy () ) {
357
360
if ( hasQueuedOperations () ) {
358
361
session .flush ();
@@ -399,6 +402,7 @@ public Object doWork() {
399
402
session .getPersistenceContextInternal ()
400
403
.getCollectionEntry ( AbstractPersistentCollection .this );
401
404
final CollectionPersister persister = entry .getLoadedPersister ();
405
+ checkPersister ( AbstractPersistentCollection .this , persister );
402
406
isExtraLazy = persister .isExtraLazy ();
403
407
if ( isExtraLazy ) {
404
408
if ( hasQueuedOperations () ) {
@@ -646,12 +650,22 @@ private void throwLazyInitializationExceptionIfNotConnected() {
646
650
}
647
651
648
652
private void throwLazyInitializationException (String message ) {
653
+ throwLazyInitializationException ( role , message );
654
+ }
655
+
656
+ private static void throwLazyInitializationException (String role , String message ) {
649
657
throw new LazyInitializationException (
650
658
String .format ( "Cannot lazily initialize collection%s (%s)" ,
651
659
role == null ? "" : " of role '" + role + "'" , message )
652
660
);
653
661
}
654
662
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
+
655
669
protected final void setInitialized () {
656
670
this .initializing = false ;
657
671
this .initialized = true ;
0 commit comments