Skip to content

Commit 47aaa7e

Browse files
Fix FetchLazyProperties updates modified properties (nhibernate#3343)
Fixes nhibernate#3330 Co-authored-by: Alexander Shutov <AlexanderShutov@yandex.ru>
1 parent 0e08e91 commit 47aaa7e

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed

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

+39
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,45 @@ public async Task TestLinqFetchAllPropertiesAsync()
274274
AssertFetchAllProperties(person);
275275
}
276276

277+
[TestCase(true)]
278+
[TestCase(false)]
279+
public async Task TestLinqFetchAllProperties_WhenLazyPropertyChangedAsync(bool initLazyPropertyFetchGroup)
280+
{
281+
Person person;
282+
using (var s = OpenSession())
283+
{
284+
person = await (s.GetAsync<Person>(1));
285+
if (initLazyPropertyFetchGroup)
286+
CollectionAssert.AreEqual(new byte[] { 0 }, person.Image);
287+
288+
person.Image = new byte[] { 1, 2, 3 };
289+
290+
var allPersons = await (s.Query<Person>().FetchLazyProperties().ToListAsync());
291+
// After execute FetchLazyProperties(), I expected to see that the person.Image would be { 1, 2, 3 }.
292+
// Because I changed this person.Image manually, I didn't want to lose those changes.
293+
// But test failed. Оld value returned { 0 }.
294+
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, person.Image);
295+
}
296+
}
297+
298+
[TestCase(true)]
299+
[TestCase(false)]
300+
public async Task TestLinqFetchProperty_WhenLazyPropertyChangedAsync(bool initLazyPropertyFetchGroup)
301+
{
302+
Person person;
303+
using (var s = OpenSession())
304+
{
305+
person = await (s.GetAsync<Person>(1));
306+
if (initLazyPropertyFetchGroup)
307+
CollectionAssert.AreEqual(new byte[] { 0 }, person.Image);
308+
309+
person.Image = new byte[] { 1, 2, 3 };
310+
311+
var allPersons = await (s.Query<Person>().Fetch(x => x.Image).ToListAsync());
312+
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, person.Image);
313+
}
314+
}
315+
277316
private static void AssertFetchAllProperties(Person person)
278317
{
279318
Assert.That(person, Is.Not.Null);

src/NHibernate.Test/FetchLazyProperties/FetchLazyPropertiesFixture.cs

+39
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,45 @@ public void TestLinqFetchAllProperties()
263263
AssertFetchAllProperties(person);
264264
}
265265

266+
[TestCase(true)]
267+
[TestCase(false)]
268+
public void TestLinqFetchAllProperties_WhenLazyPropertyChanged(bool initLazyPropertyFetchGroup)
269+
{
270+
Person person;
271+
using (var s = OpenSession())
272+
{
273+
person = s.Get<Person>(1);
274+
if (initLazyPropertyFetchGroup)
275+
CollectionAssert.AreEqual(new byte[] { 0 }, person.Image);
276+
277+
person.Image = new byte[] { 1, 2, 3 };
278+
279+
var allPersons = s.Query<Person>().FetchLazyProperties().ToList();
280+
// After execute FetchLazyProperties(), I expected to see that the person.Image would be { 1, 2, 3 }.
281+
// Because I changed this person.Image manually, I didn't want to lose those changes.
282+
// But test failed. Оld value returned { 0 }.
283+
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, person.Image);
284+
}
285+
}
286+
287+
[TestCase(true)]
288+
[TestCase(false)]
289+
public void TestLinqFetchProperty_WhenLazyPropertyChanged(bool initLazyPropertyFetchGroup)
290+
{
291+
Person person;
292+
using (var s = OpenSession())
293+
{
294+
person = s.Get<Person>(1);
295+
if (initLazyPropertyFetchGroup)
296+
CollectionAssert.AreEqual(new byte[] { 0 }, person.Image);
297+
298+
person.Image = new byte[] { 1, 2, 3 };
299+
300+
var allPersons = s.Query<Person>().Fetch(x => x.Image).ToList();
301+
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, person.Image);
302+
}
303+
}
304+
266305
private static void AssertFetchAllProperties(Person person)
267306
{
268307
Assert.That(person, Is.Not.Null);

src/NHibernate/Async/Loader/Loader.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,13 @@ private async Task UpdateLazyPropertiesFromResultSetAsync(DbDataReader rs, int i
797797
? persister.EntityMetamodel.BytecodeEnhancementMetadata.GetUninitializedLazyProperties(entry.LoadedState)
798798
: persister.EntityMetamodel.BytecodeEnhancementMetadata.GetUninitializedLazyProperties(obj);
799799

800-
var updateLazyProperties = fetchLazyProperties?.Intersect(uninitializedProperties).ToArray();
801-
if (updateLazyProperties?.Length == 0)
800+
if (uninitializedProperties.Count == 0)
801+
return;
802+
803+
var updateLazyProperties = fetchAllProperties
804+
? uninitializedProperties.ToArray()
805+
: fetchLazyProperties.Intersect(uninitializedProperties).ToArray();
806+
if (updateLazyProperties.Length == 0)
802807
{
803808
return; // No new lazy properites were loaded
804809
}
@@ -814,7 +819,7 @@ private async Task UpdateLazyPropertiesFromResultSetAsync(DbDataReader rs, int i
814819
? EntityAliases[i].SuffixedPropertyAliases
815820
: GetSubclassEntityAliases(i, persister);
816821

817-
if (!await (persister.InitializeLazyPropertiesAsync(rs, id, obj, cols, updateLazyProperties, fetchAllProperties, session, cancellationToken)).ConfigureAwait(false))
822+
if (!await (persister.InitializeLazyPropertiesAsync(rs, id, obj, cols, updateLazyProperties, false, session, cancellationToken)).ConfigureAwait(false))
818823
{
819824
return;
820825
}

src/NHibernate/Loader/Loader.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1203,8 +1203,13 @@ private void UpdateLazyPropertiesFromResultSet(DbDataReader rs, int i, object ob
12031203
? persister.EntityMetamodel.BytecodeEnhancementMetadata.GetUninitializedLazyProperties(entry.LoadedState)
12041204
: persister.EntityMetamodel.BytecodeEnhancementMetadata.GetUninitializedLazyProperties(obj);
12051205

1206-
var updateLazyProperties = fetchLazyProperties?.Intersect(uninitializedProperties).ToArray();
1207-
if (updateLazyProperties?.Length == 0)
1206+
if (uninitializedProperties.Count == 0)
1207+
return;
1208+
1209+
var updateLazyProperties = fetchAllProperties
1210+
? uninitializedProperties.ToArray()
1211+
: fetchLazyProperties.Intersect(uninitializedProperties).ToArray();
1212+
if (updateLazyProperties.Length == 0)
12081213
{
12091214
return; // No new lazy properites were loaded
12101215
}
@@ -1220,7 +1225,7 @@ private void UpdateLazyPropertiesFromResultSet(DbDataReader rs, int i, object ob
12201225
? EntityAliases[i].SuffixedPropertyAliases
12211226
: GetSubclassEntityAliases(i, persister);
12221227

1223-
if (!persister.InitializeLazyProperties(rs, id, obj, cols, updateLazyProperties, fetchAllProperties, session))
1228+
if (!persister.InitializeLazyProperties(rs, id, obj, cols, updateLazyProperties, false, session))
12241229
{
12251230
return;
12261231
}

0 commit comments

Comments
 (0)