Skip to content

Commit 342b374

Browse files
Nicaogoskarb
authored andcommitted
Fixed NH-3132
1 parent a21a150 commit 342b374

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/NHibernate.Test/NHSpecificTest/NH3132/Fixture.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,30 @@ public void Query_returns_correct_name()
8080
Assert.AreEqual("First", product.Name);
8181
}
8282
}
83+
84+
[Test]
85+
public void Correct_value_gets_saved()
86+
{
87+
using (var session = OpenSession())
88+
{
89+
Product product = session.CreateCriteria(typeof(Product))
90+
.Add(Restrictions.Eq("Name", "First"))
91+
.UniqueResult<Product>();
92+
93+
Assert.IsNotNull(product);
94+
product.Name = "Changed";
95+
96+
session.Flush();
97+
98+
session.Clear();
99+
100+
Product product1 = session.CreateCriteria(typeof(Product))
101+
.Add(Restrictions.Eq("Name", "Changed"))
102+
.UniqueResult<Product>();
103+
104+
Assert.IsNotNull(product1);
105+
Assert.AreEqual("Changed", product1.Name);
106+
}
107+
}
83108
}
84109
}

src/NHibernate/Properties/FieldAccessor.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
using System.Reflection;
44
using System.Reflection.Emit;
55
using NHibernate.Engine;
6+
using NHibernate.Intercept;
7+
using NHibernate.Proxy;
8+
using NHibernate.Proxy.DynamicProxy;
69
using NHibernate.Util;
710

811
namespace NHibernate.Properties
@@ -154,6 +157,22 @@ private string GetFieldName(string propertyName)
154157
}
155158
}
156159

160+
private static object GetTarget(object maybeProxy)
161+
{
162+
//wish there were an interface to unwrap with
163+
var proxy = maybeProxy as IProxy;
164+
if (proxy != null)
165+
{
166+
var fieldInterceptor = proxy.Interceptor as DefaultDynamicLazyFieldInterceptor;
167+
if (fieldInterceptor != null)
168+
{
169+
return fieldInterceptor.TargetInstance;
170+
}
171+
}
172+
173+
return maybeProxy;
174+
}
175+
157176
/// <summary>
158177
/// An <see cref="IGetter"/> that uses a Field instead of the Property <c>get</c>.
159178
/// </summary>
@@ -190,7 +209,7 @@ public object Get(object target)
190209
{
191210
try
192211
{
193-
return field.GetValue(target);
212+
return field.GetValue(GetTarget(target));
194213
}
195214
catch (Exception e)
196215
{
@@ -275,7 +294,7 @@ public void Set(object target, object value)
275294
{
276295
try
277296
{
278-
field.SetValue(target, value);
297+
field.SetValue(GetTarget(target), value);
279298
}
280299
catch (ArgumentException ae)
281300
{

0 commit comments

Comments
 (0)