Subject: [PATCH] Bugfix for https://github.com/nhibernate/nhibernate-core/issues/3622 --- Index: src/NHibernate/Tuple/Entity/PocoEntityTuplizer.cs IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/src/NHibernate/Tuple/Entity/PocoEntityTuplizer.cs b/src/NHibernate/Tuple/Entity/PocoEntityTuplizer.cs --- a/src/NHibernate/Tuple/Entity/PocoEntityTuplizer.cs (revision ac2ff3a0d0f3ef7385410f1d93f8dde63f2e1475) +++ b/src/NHibernate/Tuple/Entity/PocoEntityTuplizer.cs (date 1731064422202) @@ -226,7 +226,9 @@ if (IsInstrumented) { var interceptor = _enhancementMetadata.ExtractInterceptor(entity); - if (interceptor == null) + + // CHANGE + if (interceptor == null || HasAnyInitializedLazyProperty(entity, session)) { interceptor = _enhancementMetadata.InjectInterceptor(entity, session); } @@ -239,6 +241,13 @@ } } + private static bool HasAnyInitializedLazyProperty(object entity, ISessionImplementor session) + { + IPersistenceContext persistenceContext = session.PersistenceContext; + EntityEntry entityEntry = persistenceContext.GetEntry(entity); + return entityEntry.LoadedWithLazyPropertiesUnfetched; + } + public override object GetPropertyValue(object entity, int i) { if (isBytecodeProviderImpl && optimizer?.AccessOptimizer != null) Index: src/NHibernate/Loader/Loader.cs IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/src/NHibernate/Loader/Loader.cs b/src/NHibernate/Loader/Loader.cs --- a/src/NHibernate/Loader/Loader.cs (revision ac2ff3a0d0f3ef7385410f1d93f8dde63f2e1475) +++ b/src/NHibernate/Loader/Loader.cs (date 1731061210077) @@ -1143,7 +1143,8 @@ ILoadable concretePersister = GetConcretePersister(dr, i, persister, key.Identifier, session); - if (optionalObjectKey != null && key.Equals(optionalObjectKey)) + bool useOptionalObject = optionalObjectKey != null && key.Equals(optionalObjectKey); + if (useOptionalObject) { // its the given optional object obj = optionalObject; @@ -1159,8 +1160,8 @@ // (but don't yet initialize the object itself) // note that we acquired LockMode.READ even if it was not requested LockMode acquiredLockMode = lockMode == LockMode.None ? LockMode.Read : lockMode; - LoadFromResultSet(dr, i, obj, concretePersister, key, acquiredLockMode, persister, session); - + LoadFromResultSet(dr, i, obj, concretePersister, key, acquiredLockMode, persister, session, useOptionalObject); + // materialize associations (and initialize the object) later hydratedObjects.Add(obj); @@ -1287,7 +1288,7 @@ /// private void LoadFromResultSet(DbDataReader rs, int i, object obj, ILoadable persister, EntityKey key, LockMode lockMode, ILoadable rootPersister, - ISessionImplementor session) + ISessionImplementor session, bool lazyPropertiesAreUnfetched) { object id = key.Identifier; @@ -1312,7 +1313,7 @@ object rowId = persister.HasRowId ? rs[EntityAliases[i].RowIdAlias] : null; - TwoPhaseLoad.PostHydrate(persister, id, values, rowId, obj, lockMode, session); + TwoPhaseLoad.PostHydrate(persister, id, values, rowId, obj, lockMode, lazyPropertiesAreUnfetched, session); } private string[][] GetSubclassEntityAliases(int i, ILoadable persister) Index: src/NHibernate/Engine/TwoPhaseLoad.cs IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/src/NHibernate/Engine/TwoPhaseLoad.cs b/src/NHibernate/Engine/TwoPhaseLoad.cs --- a/src/NHibernate/Engine/TwoPhaseLoad.cs (revision ac2ff3a0d0f3ef7385410f1d93f8dde63f2e1475) +++ b/src/NHibernate/Engine/TwoPhaseLoad.cs (date 1731064408939) @@ -31,7 +31,7 @@ /// read from the JDBC result set we are currently processing /// // Since 5.3 - [Obsolete("Use the overload without lazyPropertiesAreUnfetched parameter instead")] + //[Obsolete("Use the overload without lazyPropertiesAreUnfetched parameter instead")] CHANGE public static void PostHydrate(IEntityPersister persister, object id, object[] values, object rowId, object obj, LockMode lockMode, bool lazyPropertiesAreUnfetched, ISessionImplementor session) { object version = Versioning.GetVersion(values, persister); Index: src/NHibernate/Engine/EntityEntry.cs IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/src/NHibernate/Engine/EntityEntry.cs b/src/NHibernate/Engine/EntityEntry.cs --- a/src/NHibernate/Engine/EntityEntry.cs (revision ac2ff3a0d0f3ef7385410f1d93f8dde63f2e1475) +++ b/src/NHibernate/Engine/EntityEntry.cs (date 1731059461196) @@ -202,7 +202,7 @@ } // Since 5.3 - [Obsolete("This property is not used and will be removed in a future version.")] + //[Obsolete("This property is not used and will be removed in a future version.")] // CHANGE public bool LoadedWithLazyPropertiesUnfetched { get { return loadedWithLazyPropertiesUnfetched; } Index: src/NHibernate/Engine/IPersistenceContext.cs IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/src/NHibernate/Engine/IPersistenceContext.cs b/src/NHibernate/Engine/IPersistenceContext.cs --- a/src/NHibernate/Engine/IPersistenceContext.cs (revision ac2ff3a0d0f3ef7385410f1d93f8dde63f2e1475) +++ b/src/NHibernate/Engine/IPersistenceContext.cs (date 1731064612460) @@ -174,7 +174,7 @@ /// to the event source's internal caches. /// // Since 5.3 - [Obsolete("Use the AddEntry extension method instead")] + //[Obsolete("Use the AddEntry extension method instead")] // CHANGE EntityEntry AddEntry(object entity, Status status, object[] loadedState, object rowId, object id, object version, LockMode lockMode, bool existsInDatabase, IEntityPersister persister, bool disableVersionIncrement, bool lazyPropertiesAreUnfetched);