Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import org.checkerframework.checker.nullness.qual.Nullable;

import static org.hibernate.internal.util.NullnessUtil.castNonNull;

/**
* Base support for CollectionInitializer implementations that represent
* an immediate initialization of some sort (join, select, batch, sub-select)
Expand Down Expand Up @@ -141,7 +143,39 @@ public void resolveState(Data data) {
public void resolveFromPreviousRow(Data data) {
super.resolveFromPreviousRow( data );
if ( data.getState() == State.RESOLVED ) {
resolveKeySubInitializers( data );
// The state is resolved, so we know a collection instance exists
final PersistentCollection<?> collection = castNonNull( data.getCollectionInstance() );
if ( collection.wasInitialized() ) {
// The collection was an already initialized instance, so we can set this to initialized
// and just resolve sub-initializers
resolveFromPreviouslyInitializedInstance( data );
}
else {
resolveKeySubInitializers( data );
}
}
}

protected void resolveFromPreviouslyInitializedInstance(Data data) {
data.setState( State.INITIALIZED );
if ( data.shallowCached ) {
initializeShallowCached( data );
}
else {
resolveInstanceSubInitializers( data );
}
final var rowProcessingState = data.getRowProcessingState();
if ( rowProcessingState.needsResolveState() ) {
// Resolve the state of the identifier if result caching is enabled and this is not a query cache hit
if ( collectionKeyResultAssembler != null ) {
collectionKeyResultAssembler.resolveState( rowProcessingState );
}
if ( !getInitializingCollectionDescriptor().useShallowQueryCacheLayout() ) {
if ( collectionValueKeyResultAssembler != null ) {
collectionValueKeyResultAssembler.resolveState( rowProcessingState );
}
resolveCollectionContentState( rowProcessingState );
}
}
}

Expand Down Expand Up @@ -337,25 +371,7 @@ public void resolveInstance(Object instance, Data data) {
}
data.collectionValueKey = null;
if ( collection.wasInitialized() ) {
data.setState( State.INITIALIZED );
if ( data.shallowCached ) {
initializeShallowCached( data );
}
else {
resolveInstanceSubInitializers( data );
}
if ( rowProcessingState.needsResolveState() ) {
// Resolve the state of the identifier if result caching is enabled and this is not a query cache hit
if ( collectionKeyResultAssembler != null ) {
collectionKeyResultAssembler.resolveState( rowProcessingState );
}
if ( !getInitializingCollectionDescriptor().useShallowQueryCacheLayout() ) {
if ( collectionValueKeyResultAssembler != null ) {
collectionValueKeyResultAssembler.resolveState( rowProcessingState );
}
resolveCollectionContentState( rowProcessingState );
}
}
resolveFromPreviouslyInitializedInstance( data );
}
else {
if ( data.shallowCached ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ public void testInitializedDetachedInstance(SessionFactoryScope scope) {
} );
}

@Test
public void testExistingPersistentInstance(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final var entityA = session.find( EntityA.class, 1L );
fetchQuery( new ArrayList<>( entityA.getB() ), session );
} );
}

@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public void testInitializedDetachedInstance(SessionFactoryScope scope) {
} );
}

@Test
public void testExistingPersistentInstance(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final var entityA = session.find( EntityA.class, 1L );
fetchQuery( new ArrayList<>( entityA.getB() ), session );
} );
}

@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Expand Down
Loading