Skip to content

Commit 6695617

Browse files
committed
HHH-17953 fix StatelessSession.fetch() for empty collection
Signed-off-by: Gavin King <gavin@hibernate.org>
1 parent c0d2075 commit 6695617

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.hibernate.internal.CoreLogging;
2121
import org.hibernate.internal.CoreMessageLogger;
2222
import org.hibernate.persister.collection.CollectionPersister;
23-
import org.hibernate.pretty.MessageHelper;
2423
import org.hibernate.sql.results.internal.ResultsHelper;
2524
import org.hibernate.stat.spi.StatisticsImplementor;
2625

@@ -39,7 +38,8 @@ public void onInitializeCollection(InitializeCollectionEvent event) throws Hiber
3938
final PersistentCollection<?> collection = event.getCollection();
4039
final SessionImplementor source = event.getSession();
4140

42-
final CollectionEntry ce = source.getPersistenceContextInternal().getCollectionEntry( collection );
41+
final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
42+
final CollectionEntry ce = persistenceContext.getCollectionEntry( collection );
4343
if ( ce == null ) {
4444
throw new HibernateException( "collection was evicted" );
4545
}
@@ -65,7 +65,7 @@ public void onInitializeCollection(InitializeCollectionEvent event) throws Hiber
6565
LOG.trace( "Collection not cached" );
6666
}
6767
loadedPersister.initialize( loadedKey, source );
68-
handlePotentiallyEmptyCollection( collection, source, ce, loadedPersister );
68+
handlePotentiallyEmptyCollection( collection, persistenceContext, loadedKey, loadedPersister );
6969
if ( LOG.isTraceEnabled() ) {
7070
LOG.trace( "Collection initialized" );
7171
}
@@ -78,18 +78,18 @@ public void onInitializeCollection(InitializeCollectionEvent event) throws Hiber
7878
}
7979
}
8080

81-
private void handlePotentiallyEmptyCollection(
81+
public static void handlePotentiallyEmptyCollection(
8282
PersistentCollection<?> collection,
83-
SessionImplementor source,
84-
CollectionEntry ce,
83+
PersistenceContext persistenceContext,
84+
Object loadedKey,
8585
CollectionPersister loadedPersister) {
8686
if ( !collection.wasInitialized() ) {
8787
collection.initializeEmptyCollection( loadedPersister );
8888
ResultsHelper.finalizeCollectionLoading(
89-
source.getPersistenceContext(),
89+
persistenceContext,
9090
loadedPersister,
9191
collection,
92-
ce.getLoadedKey(),
92+
loadedKey,
9393
true
9494
);
9595
}

hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import static org.hibernate.engine.internal.Versioning.incrementVersion;
5050
import static org.hibernate.engine.internal.Versioning.seedVersion;
5151
import static org.hibernate.engine.internal.Versioning.setVersion;
52+
import static org.hibernate.event.internal.DefaultInitializeCollectionEventListener.handlePotentiallyEmptyCollection;
5253
import static org.hibernate.generator.EventType.INSERT;
5354
import static org.hibernate.internal.util.NullnessUtil.castNonNull;
5455
import static org.hibernate.pretty.MessageHelper.infoString;
@@ -503,6 +504,8 @@ else if ( association instanceof PersistentCollection ) {
503504
persistentCollection.setCurrentSession( this );
504505
try {
505506
collectionDescriptor.initialize( key, this );
507+
handlePotentiallyEmptyCollection( persistentCollection, getPersistenceContextInternal(), key,
508+
collectionDescriptor );
506509
}
507510
finally {
508511
persistentCollection.unsetSession( this );

hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/fetch/FetchTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ public void testFetch() {
8989
long count = stats.getPrepareStatementCount();
9090
session.fetch( school.students);
9191
assertTrue( Hibernate.isInitialized( school.students) );
92+
assertEquals( 1, school.students.size() );
93+
94+
assertEquals( count+1, stats.getPrepareStatementCount() );
95+
}
96+
);
97+
}
98+
99+
@Test
100+
public void testFetchEmpty() {
101+
inStatelessTransaction(
102+
session -> {
103+
Secondary secondary = new Secondary( "BHS" );
104+
session.insert(secondary);
105+
}
106+
);
107+
108+
inStatelessSession(
109+
session -> {
110+
final Statistics stats = sessionFactory().getStatistics();
111+
stats.clear();
112+
final School school = session.get( School.class, "BHS" );
113+
assertFalse( Hibernate.isInitialized( school.students) );
114+
assertTrue( school.students instanceof PersistentSet );
115+
long count = stats.getPrepareStatementCount();
116+
session.fetch( school.students);
117+
assertTrue( Hibernate.isInitialized( school.students) );
118+
assertTrue( school.students.isEmpty() );
92119

93120
assertEquals( count+1, stats.getPrepareStatementCount() );
94121
}

0 commit comments

Comments
 (0)