Skip to content

Fix FetchLazyProperties updates modified properties #3343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 30, 2023
Merged
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 @@ -274,6 +274,45 @@ public async Task TestLinqFetchAllPropertiesAsync()
AssertFetchAllProperties(person);
}

[TestCase(true)]
[TestCase(false)]
public async Task TestLinqFetchAllProperties_WhenLazyPropertyChangedAsync(bool initLazyPropertyFetchGroup)
{
Person person;
using (var s = OpenSession())
{
person = await (s.GetAsync<Person>(1));
if (initLazyPropertyFetchGroup)
CollectionAssert.AreEqual(new byte[] { 0 }, person.Image);

person.Image = new byte[] { 1, 2, 3 };

var allPersons = await (s.Query<Person>().FetchLazyProperties().ToListAsync());
// After execute FetchLazyProperties(), I expected to see that the person.Image would be { 1, 2, 3 }.
// Because I changed this person.Image manually, I didn't want to lose those changes.
// But test failed. Оld value returned { 0 }.
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, person.Image);
}
}

[TestCase(true)]
[TestCase(false)]
public async Task TestLinqFetchProperty_WhenLazyPropertyChangedAsync(bool initLazyPropertyFetchGroup)
{
Person person;
using (var s = OpenSession())
{
person = await (s.GetAsync<Person>(1));
if (initLazyPropertyFetchGroup)
CollectionAssert.AreEqual(new byte[] { 0 }, person.Image);

person.Image = new byte[] { 1, 2, 3 };

var allPersons = await (s.Query<Person>().Fetch(x => x.Image).ToListAsync());
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, person.Image);
}
}

private static void AssertFetchAllProperties(Person person)
{
Assert.That(person, Is.Not.Null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,45 @@ public void TestLinqFetchAllProperties()
AssertFetchAllProperties(person);
}

[TestCase(true)]
[TestCase(false)]
public void TestLinqFetchAllProperties_WhenLazyPropertyChanged(bool initLazyPropertyFetchGroup)
{
Person person;
using (var s = OpenSession())
{
person = s.Get<Person>(1);
if (initLazyPropertyFetchGroup)
CollectionAssert.AreEqual(new byte[] { 0 }, person.Image);

person.Image = new byte[] { 1, 2, 3 };

var allPersons = s.Query<Person>().FetchLazyProperties().ToList();
// After execute FetchLazyProperties(), I expected to see that the person.Image would be { 1, 2, 3 }.
// Because I changed this person.Image manually, I didn't want to lose those changes.
// But test failed. Оld value returned { 0 }.
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, person.Image);
}
}

[TestCase(true)]
[TestCase(false)]
public void TestLinqFetchProperty_WhenLazyPropertyChanged(bool initLazyPropertyFetchGroup)
{
Person person;
using (var s = OpenSession())
{
person = s.Get<Person>(1);
if (initLazyPropertyFetchGroup)
CollectionAssert.AreEqual(new byte[] { 0 }, person.Image);

person.Image = new byte[] { 1, 2, 3 };

var allPersons = s.Query<Person>().Fetch(x => x.Image).ToList();
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, person.Image);
}
}

private static void AssertFetchAllProperties(Person person)
{
Assert.That(person, Is.Not.Null);
Expand Down
11 changes: 8 additions & 3 deletions src/NHibernate/Async/Loader/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,13 @@ private async Task UpdateLazyPropertiesFromResultSetAsync(DbDataReader rs, int i
? persister.EntityMetamodel.BytecodeEnhancementMetadata.GetUninitializedLazyProperties(entry.LoadedState)
: persister.EntityMetamodel.BytecodeEnhancementMetadata.GetUninitializedLazyProperties(obj);

var updateLazyProperties = fetchLazyProperties?.Intersect(uninitializedProperties).ToArray();
if (updateLazyProperties?.Length == 0)
if (uninitializedProperties.Count == 0)
return;

var updateLazyProperties = fetchAllProperties
? uninitializedProperties.ToArray()
: fetchLazyProperties.Intersect(uninitializedProperties).ToArray();
if (updateLazyProperties.Length == 0)
{
return; // No new lazy properites were loaded
}
Expand All @@ -814,7 +819,7 @@ private async Task UpdateLazyPropertiesFromResultSetAsync(DbDataReader rs, int i
? EntityAliases[i].SuffixedPropertyAliases
: GetSubclassEntityAliases(i, persister);

if (!await (persister.InitializeLazyPropertiesAsync(rs, id, obj, cols, updateLazyProperties, fetchAllProperties, session, cancellationToken)).ConfigureAwait(false))
if (!await (persister.InitializeLazyPropertiesAsync(rs, id, obj, cols, updateLazyProperties, false, session, cancellationToken)).ConfigureAwait(false))
{
return;
}
Expand Down
11 changes: 8 additions & 3 deletions src/NHibernate/Loader/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,8 +1203,13 @@ private void UpdateLazyPropertiesFromResultSet(DbDataReader rs, int i, object ob
? persister.EntityMetamodel.BytecodeEnhancementMetadata.GetUninitializedLazyProperties(entry.LoadedState)
: persister.EntityMetamodel.BytecodeEnhancementMetadata.GetUninitializedLazyProperties(obj);

var updateLazyProperties = fetchLazyProperties?.Intersect(uninitializedProperties).ToArray();
if (updateLazyProperties?.Length == 0)
if (uninitializedProperties.Count == 0)
return;

var updateLazyProperties = fetchAllProperties
? uninitializedProperties.ToArray()
: fetchLazyProperties.Intersect(uninitializedProperties).ToArray();
if (updateLazyProperties.Length == 0)
{
return; // No new lazy properites were loaded
}
Expand All @@ -1220,7 +1225,7 @@ private void UpdateLazyPropertiesFromResultSet(DbDataReader rs, int i, object ob
? EntityAliases[i].SuffixedPropertyAliases
: GetSubclassEntityAliases(i, persister);

if (!persister.InitializeLazyProperties(rs, id, obj, cols, updateLazyProperties, fetchAllProperties, session))
if (!persister.InitializeLazyProperties(rs, id, obj, cols, updateLazyProperties, false, session))
{
return;
}
Expand Down