Skip to content

Commit b443a86

Browse files
Adjustment for Join-fetching with property-ref results in n+1 select
1 parent 3230a62 commit b443a86

File tree

2 files changed

+7
-23
lines changed

2 files changed

+7
-23
lines changed

src/NHibernate/Async/Loader/Loader.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -651,28 +651,7 @@ private async Task InstanceAlreadyLoadedAsync(DbDataReader rs, int i, IEntityPer
651651
}
652652
}
653653

654-
// #1226: If it is already loaded and can be loaded from an association with a property ref, make
655-
// sure it is also cached by its unique key.
656-
var ukName = OwnerAssociationTypes?[i]?.RHSUniqueKeyPropertyName;
657-
if (ukName == null)
658-
return;
659-
var index = ((IUniqueKeyLoadable) persister).GetPropertyIndex(ukName);
660-
var ukValue = persister.GetPropertyValue(obj, index);
661-
// ukValue can be null for two reasons:
662-
// - Entity currently loading and not yet fully hydrated. In such case, it has already been handled by
663-
// InstanceNotYetLoaded on a previous row, there is nothing more to do. This case could also be
664-
// detected with "session.PersistenceContext.GetEntry(obj).Status == Status.Loading", but since there
665-
// is a second case, just test for ukValue null.
666-
// - Entity association is unset in session but not yet persisted, autoflush disabled: ignore. We are
667-
// already in an error case: querying entities changed in session without flushing them before querying.
668-
// So here it gets loaded as if it were still associated, but we do not have the key anymore in session:
669-
// we cannot cache it, so long for the additionnal round-trip this will cause. (Do not fallback on
670-
// reading the key in rs, this is stale data in regard to the session state.)
671-
if (ukValue == null)
672-
return;
673-
var type = persister.PropertyTypes[index];
674-
var euk = new EntityUniqueKey(persister.EntityName, ukName, ukValue, type, session.Factory);
675-
session.PersistenceContext.AddEntity(euk, obj);
654+
CacheByUniqueKey(i, persister, obj, session);
676655
}
677656

678657
/// <summary>

src/NHibernate/Loader/Loader.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,12 +973,17 @@ private void InstanceAlreadyLoaded(DbDataReader rs, int i, IEntityPersister pers
973973
}
974974
}
975975

976+
CacheByUniqueKey(i, persister, obj, session);
977+
}
978+
979+
private void CacheByUniqueKey(int i, IEntityPersister persister, object obj, ISessionImplementor session)
980+
{
976981
// #1226: If it is already loaded and can be loaded from an association with a property ref, make
977982
// sure it is also cached by its unique key.
978983
var ukName = OwnerAssociationTypes?[i]?.RHSUniqueKeyPropertyName;
979984
if (ukName == null)
980985
return;
981-
var index = ((IUniqueKeyLoadable) persister).GetPropertyIndex(ukName);
986+
var index = ((IUniqueKeyLoadable)persister).GetPropertyIndex(ukName);
982987
var ukValue = persister.GetPropertyValue(obj, index);
983988
// ukValue can be null for two reasons:
984989
// - Entity currently loading and not yet fully hydrated. In such case, it has already been handled by

0 commit comments

Comments
 (0)