Skip to content

Commit 66fdefc

Browse files
author
Danko Durbić
committed
Simplifed fix for fetching all lazy properties
1 parent b0fcac1 commit 66fdefc

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/NHibernate.Test/Async/FetchLazyProperties/FetchLazyPropertiesFixture.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,26 @@ public async Task TestFetchAfterEntityIsInitializedAsync(bool readOnly)
945945
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Formula"), Is.True);
946946
}
947947

948+
[TestCase(true)]
949+
[TestCase(false)]
950+
public async Task TestFetchAllPropertiesAfterEntityIsInitializedAsync(bool readOnly)
951+
{
952+
Person person;
953+
using(var s = OpenSession())
954+
using(var tx = s.BeginTransaction())
955+
{
956+
person = await (s.CreateQuery("from Person where Id = 1").SetReadOnly(readOnly).UniqueResultAsync<Person>());
957+
var image = person.Image;
958+
person = await (s.CreateQuery("from Person fetch all properties where Id = 1").SetReadOnly(readOnly).UniqueResultAsync<Person>());
959+
960+
await (tx.CommitAsync());
961+
}
962+
963+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.True);
964+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.True);
965+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Formula"), Is.True);
966+
}
967+
948968
[Test]
949969
public async Task TestHqlCrossJoinFetchFormulaAsync()
950970
{

src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,18 +1408,31 @@ public void InitializeLazyProperties(
14081408
{
14091409
throw new HibernateException($"Entity is not associated with the session: {id}");
14101410
}
1411-
1412-
var lazyProperties = allLazyProperties ? LazyProperties.ToArray() : uninitializedLazyProperties;
1413-
var metadata = InstrumentationMetadata.LazyPropertiesMetadata;
1414-
var indexes = new int[lazyProperties.Length];
1415-
var lazyIndexes = new int[lazyProperties.Length];
1416-
for (var i = 0; i < lazyProperties.Length; i++)
1411+
1412+
int[] indexes;
1413+
int[] lazyIndexes;
1414+
if (allLazyProperties)
14171415
{
1418-
var descriptor = metadata.GetLazyPropertyDescriptor(lazyProperties[i]);
1419-
indexes[i] = descriptor.PropertyIndex;
1420-
lazyIndexes[i] = descriptor.LazyIndex;
1416+
indexes = lazyPropertyNumbers;
1417+
lazyIndexes = new int[lazyPropertyNumbers.Length];
1418+
for(var i = 0; i < lazyIndexes.Length; i++)
1419+
{
1420+
lazyIndexes[i] = i;
1421+
}
14211422
}
1422-
1423+
else
1424+
{
1425+
var metadata = InstrumentationMetadata.LazyPropertiesMetadata;
1426+
indexes = new int[uninitializedLazyProperties.Length];
1427+
lazyIndexes = new int[uninitializedLazyProperties.Length];
1428+
for(var i = 0; i < uninitializedLazyProperties.Length; i++)
1429+
{
1430+
var descriptor = metadata.GetLazyPropertyDescriptor(uninitializedLazyProperties[i]);
1431+
indexes[i] = descriptor.PropertyIndex;
1432+
lazyIndexes[i] = descriptor.LazyIndex;
1433+
}
1434+
}
1435+
14231436
var values = Hydrate(rs, id, entity, suffixedPropertyColumns, null, true, indexes, session);
14241437
for (var i = 0; i < lazyIndexes.Length; i++)
14251438
{

0 commit comments

Comments
 (0)