@@ -195,8 +195,8 @@ public int getSize() {
195
195
final CollectionEntry entry = session .getPersistenceContextInternal ().getCollectionEntry ( this );
196
196
if ( entry == null ) {
197
197
throwLazyInitializationExceptionIfNotConnected ();
198
- throwLazyInitializationException ("collection not associated with session" );
199
- throw new AssertionFailure ("impossible" );
198
+ throwLazyInitializationException ( "collection not associated with session" );
199
+ throw new AssertionFailure ( "impossible" );
200
200
}
201
201
else {
202
202
if ( hasQueuedOperations () ) {
@@ -233,23 +233,23 @@ private <T> T withTemporarySessionIfNeeded(LazyInitializationWork<T> lazyInitial
233
233
tempSession = openTemporarySessionForLoading ();
234
234
}
235
235
else {
236
- throwLazyInitializationException ( "could not initialize proxy - no Session " );
236
+ throwLazyInitializationException ( "no session " );
237
237
}
238
238
}
239
239
else if ( !session .isOpenOrWaitingForAutoClose () ) {
240
240
if ( allowLoadOutsideTransaction ) {
241
241
tempSession = openTemporarySessionForLoading ();
242
242
}
243
243
else {
244
- throwLazyInitializationException ( "could not initialize proxy - the owning Session was closed" );
244
+ throwLazyInitializationException ( "the owning session was closed" );
245
245
}
246
246
}
247
247
else if ( !session .isConnected () ) {
248
248
if ( allowLoadOutsideTransaction ) {
249
249
tempSession = openTemporarySessionForLoading ();
250
250
}
251
251
else {
252
- throwLazyInitializationException ( "could not initialize proxy - the owning Session is disconnected" );
252
+ throwLazyInitializationException ( "the owning session is disconnected" );
253
253
}
254
254
}
255
255
@@ -273,8 +273,7 @@ else if ( !session.isConnected() ) {
273
273
}
274
274
275
275
final CollectionPersister collectionDescriptor =
276
- session .getSessionFactory ()
277
- .getMappingMetamodel ()
276
+ session .getSessionFactory ().getMappingMetamodel ()
278
277
.getCollectionDescriptor ( getRole () );
279
278
session .getPersistenceContextInternal ()
280
279
.addUninitializedDetachedCollection ( collectionDescriptor , this );
@@ -301,20 +300,22 @@ else if ( !session.isConnected() ) {
301
300
else {
302
301
// Whenever the collection lazy loading is triggered during the loading process,
303
302
// closing the connection will cause an error when RowProcessingStateStandardImpl#next() will be called.
304
- final PersistenceContext persistenceContext = session .getPersistenceContext ();
305
- if ( !session .isTransactionInProgress ()
306
- && ( !persistenceContext .hasLoadContext ()
307
- || persistenceContext .hasLoadContext ()
308
- && persistenceContext .getLoadContexts ().isLoadingFinished () ) ) {
303
+ if ( !session .isTransactionInProgress () && !unfinishedLoading () ) {
309
304
session .getJdbcCoordinator ().afterTransaction ();
310
305
}
311
306
}
312
307
}
313
308
}
314
309
310
+ private boolean unfinishedLoading () {
311
+ final PersistenceContext persistenceContext = session .getPersistenceContext ();
312
+ return persistenceContext .hasLoadContext ()
313
+ && !persistenceContext .getLoadContexts ().isLoadingFinished ();
314
+ }
315
+
315
316
private SharedSessionContractImplementor openTemporarySessionForLoading () {
316
317
if ( sessionFactoryUuid == null ) {
317
- throwLazyInitializationException ( "SessionFactory UUID not known to create temporary Session for loading" );
318
+ throwLazyInitializationException ( "SessionFactory UUID not known; cannot create temporary session for loading" );
318
319
}
319
320
320
321
final SessionImplementor session =
@@ -373,8 +374,8 @@ public boolean elementExists(Object element) {
373
374
final CollectionEntry entry = session .getPersistenceContextInternal ().getCollectionEntry ( this );
374
375
if ( entry == null ) {
375
376
throwLazyInitializationExceptionIfNotConnected ();
376
- throwLazyInitializationException ("collection not associated with session" );
377
- throw new AssertionFailure ("impossible" );
377
+ throwLazyInitializationException ( "collection not associated with session" );
378
+ throw new AssertionFailure ( "impossible" );
378
379
}
379
380
else {
380
381
if ( hasQueuedOperations () ) {
@@ -427,8 +428,8 @@ public Object elementByIndex(Object index) {
427
428
final CollectionEntry entry = session .getPersistenceContextInternal ().getCollectionEntry ( this );
428
429
if ( entry == null ) {
429
430
throwLazyInitializationExceptionIfNotConnected ();
430
- throwLazyInitializationException ("collection not associated with session" );
431
- throw new AssertionFailure ("impossible" );
431
+ throwLazyInitializationException ( "collection not associated with session" );
432
+ throw new AssertionFailure ( "impossible" );
432
433
}
433
434
else {
434
435
if ( hasQueuedOperations () ) {
@@ -646,9 +647,8 @@ private void throwLazyInitializationExceptionIfNotConnected() {
646
647
647
648
private void throwLazyInitializationException (String message ) {
648
649
throw new LazyInitializationException (
649
- "failed to lazily initialize a collection" +
650
- (role == null ? "" : " of role: " + role ) +
651
- ": " + message
650
+ String .format ( "Cannot lazily initialize collection%s (%s)" ,
651
+ role == null ? "" : " of role '" + role + "'" , message )
652
652
);
653
653
}
654
654
@@ -1305,11 +1305,7 @@ protected static <E> Collection<E> getOrphans(
1305
1305
currentSaving .add ( current );
1306
1306
}
1307
1307
else {
1308
- final Object currentId = getEntityIdentifierIfNotUnsaved (
1309
- entityName ,
1310
- current ,
1311
- session
1312
- );
1308
+ final Object currentId = getEntityIdentifierIfNotUnsaved ( entityName , current , session );
1313
1309
currentIds .add ( useIdDirect ? currentId : new TypedValue ( idType , currentId ) );
1314
1310
}
1315
1311
}
0 commit comments