Skip to content

Commit bb1d5fb

Browse files
stuartcarniehazzik
authored andcommitted
FIX NH-2898: Fix CastException when assembling object from 2nd level cache with lazy properties
(cherry picked from commit d01cdfb)
1 parent 4f73dab commit bb1d5fb

File tree

8 files changed

+17
-16
lines changed

8 files changed

+17
-16
lines changed

src/NHibernate/Engine/ForeignKeys.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public static bool IsNotTransient(string entityName, System.Object entity, bool?
177177
/// </remarks>
178178
public static bool IsTransient(string entityName, object entity, bool? assumed, ISessionImplementor session)
179179
{
180-
if (entity == Intercept.LazyPropertyInitializer.UnfetchedProperty)
180+
if (Equals(Intercept.LazyPropertyInitializer.UnfetchedProperty, entity))
181181
{
182182
// an unfetched association can only point to
183183
// an entity that already exists in the db

src/NHibernate/Engine/TwoPhaseLoad.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static void InitializeEntity(object entity, bool readOnly, ISessionImplem
7474
for (int i = 0; i < hydratedState.Length; i++)
7575
{
7676
object value = hydratedState[i];
77-
if (value != LazyPropertyInitializer.UnfetchedProperty && value != BackrefPropertyAccessor.Unknown)
77+
if (!Equals(LazyPropertyInitializer.UnfetchedProperty, value) && !(Equals(BackrefPropertyAccessor.Unknown, value)))
7878
{
7979
hydratedState[i] = types[i].ResolveIdentifier(value, session, entity);
8080
}

src/NHibernate/Event/Default/AbstractVisitor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ internal virtual bool IncludeEntityProperty(object[] values, int i)
129129

130130
internal bool IncludeProperty(object[] values, int i)
131131
{
132-
return values[i] != Intercept.LazyPropertyInitializer.UnfetchedProperty;
132+
return !Equals(Intercept.LazyPropertyInitializer.UnfetchedProperty, values[i]);
133133
}
134134
}
135135
}

src/NHibernate/Intercept/ILazyPropertyInitializer.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
using System;
12
using NHibernate.Engine;
23

34
namespace NHibernate.Intercept
45
{
56
public struct LazyPropertyInitializer
67
{
78
/// <summary> Marker value for uninitialized properties</summary>
8-
public readonly static object UnfetchedProperty= new object();
9+
public readonly static object UnfetchedProperty = new Guid(0x8cbd57d5, 0xe58, 0x48a6, 0xa8, 0xf3, 0xec, 0xd4, 0xc2, 0x16, 0x35, 0xee);
910
}
1011

1112
/// <summary> Contract for controlling how lazy properties get initialized. </summary>

src/NHibernate/Properties/BackrefPropertyAccessor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace NHibernate.Properties
99
[Serializable]
1010
public class BackrefPropertyAccessor : IPropertyAccessor
1111
{
12-
public static readonly object Unknown = new object();
12+
public static readonly object Unknown = new Guid(0x3dbd938f, 0xb1f8, 0x4a98, 0xa2, 0xf9, 0x62, 0xbd, 0x3b, 0x2, 0x8d, 0xcf);
1313
private readonly string propertyName;
1414
private readonly string entityName;
1515

src/NHibernate/Tuple/Component/PocoComponentTuplizer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public override System.Type MappedClass
5858
public override object[] GetPropertyValues(object component)
5959
{
6060
// NH Different behavior : for NH-1101
61-
if (component == BackrefPropertyAccessor.Unknown || component == null)
61+
if (Equals(BackrefPropertyAccessor.Unknown, component) || component == null)
6262
{
6363
return new object[propertySpan];
6464
}

src/NHibernate/Tuple/Entity/AbstractEntityTuplizer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public virtual void SetPropertyValues(object entity, object[] values)
275275

276276
for (int j = 0; j < entityMetamodel.PropertySpan; j++)
277277
{
278-
if (setAll || values[j] != LazyPropertyInitializer.UnfetchedProperty)
278+
if (setAll || !Equals(LazyPropertyInitializer.UnfetchedProperty, values[j]))
279279
{
280280
setters[j].Set(entity, values[j]);
281281
}

src/NHibernate/Type/TypeHelper.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static void DeepCopy(object[] values, IType[] types, bool[] copy, object[
2424
{
2525
if (copy[i])
2626
{
27-
if (values[i] == LazyPropertyInitializer.UnfetchedProperty || values[i] == BackrefPropertyAccessor.Unknown)
27+
if (Equals(LazyPropertyInitializer.UnfetchedProperty, values[i]) || Equals(BackrefPropertyAccessor.Unknown, values[i]))
2828
{
2929
target[i] = values[i];
3030
}
@@ -44,7 +44,7 @@ public static void BeforeAssemble(object[] row, ICacheAssembler[] types, ISessio
4444
{
4545
for (int i = 0; i < types.Length; i++)
4646
{
47-
if (row[i] != LazyPropertyInitializer.UnfetchedProperty && row[i] != BackrefPropertyAccessor.Unknown)
47+
if (!Equals(LazyPropertyInitializer.UnfetchedProperty, row[i]) && !Equals(BackrefPropertyAccessor.Unknown, row[i]))
4848
{
4949
types[i].BeforeAssemble(row[i], session);
5050
}
@@ -64,7 +64,7 @@ public static object[] Assemble(object[] row, ICacheAssembler[] types, ISessionI
6464
var assembled = new object[row.Length];
6565
for (int i = 0; i < row.Length; i++)
6666
{
67-
if (row[i] == LazyPropertyInitializer.UnfetchedProperty || row[i] == BackrefPropertyAccessor.Unknown)
67+
if (Equals(LazyPropertyInitializer.UnfetchedProperty, row[i]) || Equals(BackrefPropertyAccessor.Unknown, row[i]))
6868
{
6969
assembled[i] = row[i];
7070
}
@@ -92,7 +92,7 @@ public static object[] Disassemble(object[] row, ICacheAssembler[] types, bool[]
9292
{
9393
disassembled[i] = LazyPropertyInitializer.UnfetchedProperty;
9494
}
95-
else if (row[i] == LazyPropertyInitializer.UnfetchedProperty || row[i] == BackrefPropertyAccessor.Unknown)
95+
else if (Equals(LazyPropertyInitializer.UnfetchedProperty, row[i]) || Equals(BackrefPropertyAccessor.Unknown, row[i]))
9696
{
9797
disassembled[i] = row[i];
9898
}
@@ -120,7 +120,7 @@ public static object[] Replace(object[] original, object[] target, IType[] types
120120
var copied = new object[original.Length];
121121
for (int i = 0; i < original.Length; i++)
122122
{
123-
if (original[i] == LazyPropertyInitializer.UnfetchedProperty || original[i] == BackrefPropertyAccessor.Unknown)
123+
if (Equals(LazyPropertyInitializer.UnfetchedProperty, original[i]) || Equals(BackrefPropertyAccessor.Unknown, original[i]))
124124
{
125125
copied[i] = target[i];
126126
}
@@ -150,7 +150,7 @@ public static object[] Replace(object[] original, object[] target, IType[] types
150150
object[] copied = new object[original.Length];
151151
for (int i = 0; i < types.Length; i++)
152152
{
153-
if (original[i] == LazyPropertyInitializer.UnfetchedProperty || original[i] == BackrefPropertyAccessor.Unknown)
153+
if (Equals(LazyPropertyInitializer.UnfetchedProperty, original[i]) || Equals(BackrefPropertyAccessor.Unknown, original[i]))
154154
{
155155
copied[i] = target[i];
156156
}
@@ -182,7 +182,7 @@ public static object[] ReplaceAssociations(object[] original, object[] target, I
182182
object[] copied = new object[original.Length];
183183
for (int i = 0; i < types.Length; i++)
184184
{
185-
if (original[i] == LazyPropertyInitializer.UnfetchedProperty || original[i] == BackrefPropertyAccessor.Unknown)
185+
if (Equals(LazyPropertyInitializer.UnfetchedProperty, original[i]) || Equals(BackrefPropertyAccessor.Unknown, original[i]))
186186
{
187187
copied[i] = target[i];
188188
}
@@ -239,7 +239,7 @@ public static int[] FindDirty(StandardProperty[] properties,
239239
for (int i = 0; i < span; i++)
240240
{
241241
bool dirty =
242-
currentState[i] != LazyPropertyInitializer.UnfetchedProperty &&
242+
!Equals(LazyPropertyInitializer.UnfetchedProperty, currentState[i]) &&
243243
properties[i].IsDirtyCheckable(anyUninitializedProperties)
244244
&& properties[i].Type.IsDirty(previousState[i], currentState[i], includeColumns[i], session);
245245

@@ -290,7 +290,7 @@ public static int[] FindModified(StandardProperty[] properties,
290290
for (int i = 0; i < span; i++)
291291
{
292292
bool dirty =
293-
currentState[i] != LazyPropertyInitializer.UnfetchedProperty &&
293+
!Equals(LazyPropertyInitializer.UnfetchedProperty, currentState[i]) &&
294294
properties[i].IsDirtyCheckable(anyUninitializedProperties)
295295
&& properties[i].Type.IsModified(previousState[i], currentState[i], includeColumns[i], session);
296296

0 commit comments

Comments
 (0)