Skip to content

Commit a264f23

Browse files
authored
NH-3374 - Merge detached entity failed when the instrumented lazy property is initialized (#638)
1 parent b13c5b8 commit a264f23

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/NHibernate.Test/NHSpecificTest/NH3374/FixtureByCode.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace NHibernate.Test.NHSpecificTest.NH3374
88
{
9-
[Ignore("Not fixed yet.")]
9+
[TestFixture]
1010
public class ByCodeFixture : TestCaseMappingByCode
1111
{
1212
protected override HbmMapping GetMappings()
@@ -27,11 +27,7 @@ protected override HbmMapping GetMappings()
2727
map.Id(x => x.Id, idMapper => idMapper.Generator(Generators.Identity));
2828
map.Property(x => x.Bytes, y =>
2929
{
30-
y.Column(x =>
31-
{
32-
x.SqlType("varbinary(max)");
33-
x.Length(int.MaxValue);
34-
});
30+
y.Length(int.MaxValue);
3531
y.Lazy(true);
3632
});
3733
});

src/NHibernate/Type/TypeHelper.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ public static object[] Replace(object[] original, object[] target, IType[] types
126126
{
127127
copied[i] = target[i];
128128
}
129+
else if (target[i] == LazyPropertyInitializer.UnfetchedProperty)
130+
{
131+
// Should be no need to check for target[i] == PropertyAccessStrategyBackRefImpl.UNKNOWN
132+
// because PropertyAccessStrategyBackRefImpl.get( object ) returns
133+
// PropertyAccessStrategyBackRefImpl.UNKNOWN, so target[i] == original[i].
134+
//
135+
// We know from above that original[i] != LazyPropertyInitializer.UNFETCHED_PROPERTY &&
136+
// original[i] != PropertyAccessStrategyBackRefImpl.UNKNOWN;
137+
// This is a case where the entity being merged has a lazy property
138+
// that has been initialized. Copy the initialized value from original.
139+
if (types[i].IsMutable)
140+
{
141+
copied[i] = types[i].DeepCopy(original[i], session.Factory);
142+
}
143+
else
144+
{
145+
copied[i] = original[i];
146+
}
147+
}
129148
else
130149
{
131150
copied[i] = types[i].Replace(original[i], target[i], session, owner, copiedAlready);

0 commit comments

Comments
 (0)