Skip to content

Commit 8b1d7ab

Browse files
committed
slightly improve error messages from AbstractPersistentCollection
1 parent 8527a5b commit 8b1d7ab

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ public int getSize() {
195195
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( this );
196196
if ( entry == null ) {
197197
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" );
200200
}
201201
else {
202202
if ( hasQueuedOperations() ) {
@@ -233,23 +233,23 @@ private <T> T withTemporarySessionIfNeeded(LazyInitializationWork<T> lazyInitial
233233
tempSession = openTemporarySessionForLoading();
234234
}
235235
else {
236-
throwLazyInitializationException( "could not initialize proxy - no Session" );
236+
throwLazyInitializationException( "no session" );
237237
}
238238
}
239239
else if ( !session.isOpenOrWaitingForAutoClose() ) {
240240
if ( allowLoadOutsideTransaction ) {
241241
tempSession = openTemporarySessionForLoading();
242242
}
243243
else {
244-
throwLazyInitializationException( "could not initialize proxy - the owning Session was closed" );
244+
throwLazyInitializationException( "the owning session was closed" );
245245
}
246246
}
247247
else if ( !session.isConnected() ) {
248248
if ( allowLoadOutsideTransaction ) {
249249
tempSession = openTemporarySessionForLoading();
250250
}
251251
else {
252-
throwLazyInitializationException( "could not initialize proxy - the owning Session is disconnected" );
252+
throwLazyInitializationException( "the owning session is disconnected" );
253253
}
254254
}
255255

@@ -273,8 +273,7 @@ else if ( !session.isConnected() ) {
273273
}
274274

275275
final CollectionPersister collectionDescriptor =
276-
session.getSessionFactory()
277-
.getMappingMetamodel()
276+
session.getSessionFactory().getMappingMetamodel()
278277
.getCollectionDescriptor( getRole() );
279278
session.getPersistenceContextInternal()
280279
.addUninitializedDetachedCollection( collectionDescriptor, this );
@@ -301,20 +300,22 @@ else if ( !session.isConnected() ) {
301300
else {
302301
// Whenever the collection lazy loading is triggered during the loading process,
303302
// 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() ) {
309304
session.getJdbcCoordinator().afterTransaction();
310305
}
311306
}
312307
}
313308
}
314309

310+
private boolean unfinishedLoading() {
311+
final PersistenceContext persistenceContext = session.getPersistenceContext();
312+
return persistenceContext.hasLoadContext()
313+
&& !persistenceContext.getLoadContexts().isLoadingFinished();
314+
}
315+
315316
private SharedSessionContractImplementor openTemporarySessionForLoading() {
316317
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" );
318319
}
319320

320321
final SessionImplementor session =
@@ -373,8 +374,8 @@ public boolean elementExists(Object element) {
373374
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( this );
374375
if ( entry == null ) {
375376
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" );
378379
}
379380
else {
380381
if ( hasQueuedOperations() ) {
@@ -427,8 +428,8 @@ public Object elementByIndex(Object index) {
427428
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( this );
428429
if ( entry == null ) {
429430
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" );
432433
}
433434
else {
434435
if ( hasQueuedOperations() ) {
@@ -646,9 +647,8 @@ private void throwLazyInitializationExceptionIfNotConnected() {
646647

647648
private void throwLazyInitializationException(String message) {
648649
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 )
652652
);
653653
}
654654

@@ -1305,11 +1305,7 @@ protected static <E> Collection<E> getOrphans(
13051305
currentSaving.add( current );
13061306
}
13071307
else {
1308-
final Object currentId = getEntityIdentifierIfNotUnsaved(
1309-
entityName,
1310-
current,
1311-
session
1312-
);
1308+
final Object currentId = getEntityIdentifierIfNotUnsaved( entityName, current, session );
13131309
currentIds.add( useIdDirect ? currentId : new TypedValue( idType, currentId ) );
13141310
}
13151311
}

0 commit comments

Comments
 (0)