Skip to content

Commit 688d2ea

Browse files
jrenaatgavinking
authored andcommitted
HHH-19631 - Improve error handling of null Persister in lazy loading collections
Signed-off-by: Jan Schatteman <[email protected]>
1 parent 2e9eac2 commit 688d2ea

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultInitializeCollectionEventListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.hibernate.sql.results.internal.ResultsHelper;
1818
import org.hibernate.stat.spi.StatisticsImplementor;
1919

20+
import static org.hibernate.collection.spi.AbstractPersistentCollection.checkPersister;
2021
import static org.hibernate.loader.internal.CacheLoadHelper.initializeCollectionFromCache;
2122
import static org.hibernate.pretty.MessageHelper.collectionInfoString;
2223

@@ -41,6 +42,7 @@ public void onInitializeCollection(InitializeCollectionEvent event) throws Hiber
4142
}
4243
if ( !collection.wasInitialized() ) {
4344
final CollectionPersister loadedPersister = ce.getLoadedPersister();
45+
checkPersister(collection, loadedPersister);
4446
final Object loadedKey = ce.getLoadedKey();
4547
if ( LOG.isTraceEnabled() ) {
4648
LOG.trace( "Initializing collection "

0 commit comments

Comments
 (0)