|
From: <fab...@us...> - 2011-03-21 20:09:24
|
Revision: 5497
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5497&view=rev
Author: fabiomaulo
Date: 2011-03-21 20:09:18 +0000 (Mon, 21 Mar 2011)
Log Message:
-----------
Fix NH-2498
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Cfg/XmlHbmBinding/ClassBinder.cs
trunk/nhibernate/src/NHibernate/Intercept/AbstractFieldInterceptor.cs
trunk/nhibernate/src/NHibernate.Test/GhostProperty/GhostPropertyFixture.cs
trunk/nhibernate/src/NHibernate.Test/GhostProperty/Mappings.hbm.xml
trunk/nhibernate/src/NHibernate.Test/GhostProperty/Order.cs
Modified: trunk/nhibernate/src/NHibernate/Cfg/XmlHbmBinding/ClassBinder.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Cfg/XmlHbmBinding/ClassBinder.cs 2011-03-21 18:08:32 UTC (rev 5496)
+++ trunk/nhibernate/src/NHibernate/Cfg/XmlHbmBinding/ClassBinder.cs 2011-03-21 20:09:18 UTC (rev 5497)
@@ -439,8 +439,9 @@
// Note : fetch="join" overrides default laziness
bool isLazyTrue = !laziness.HasValue
? defaultLazy && fetchable.IsLazy
- : laziness == HbmLaziness.Proxy;
+ : (laziness == HbmLaziness.Proxy || laziness == HbmLaziness.NoProxy);
fetchable.IsLazy = isLazyTrue;
+ fetchable.UnwrapProxy = laziness == HbmLaziness.NoProxy;
}
protected void InitOuterJoinFetchSetting(HbmManyToMany manyToMany, IFetchable model)
Modified: trunk/nhibernate/src/NHibernate/Intercept/AbstractFieldInterceptor.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Intercept/AbstractFieldInterceptor.cs 2011-03-21 18:08:32 UTC (rev 5496)
+++ trunk/nhibernate/src/NHibernate/Intercept/AbstractFieldInterceptor.cs 2011-03-21 20:09:18 UTC (rev 5497)
@@ -26,7 +26,7 @@
{
this.session = session;
this.uninitializedFields = uninitializedFields;
- this.unwrapProxyFieldNames = unwrapProxyFieldNames;
+ this.unwrapProxyFieldNames = unwrapProxyFieldNames ?? new HashedSet<string>();
this.entityName = entityName;
this.mappedClass = mappedClass;
}
@@ -50,11 +50,7 @@
public bool IsInitializedField(string field)
{
- if (unwrapProxyFieldNames != null && unwrapProxyFieldNames.Contains(field))
- {
- return loadedUnwrapProxyFieldNames.Contains(field);
- }
- return uninitializedFields == null || !uninitializedFields.Contains(field);
+ return !IsUninitializedProperty(field) && !IsUninitializedAssociation(field);
}
public void MarkDirty()
@@ -89,14 +85,16 @@
get { return initializing; }
}
- // NH Specific: Hibernate only deals with lazy properties here, we deal with
- // both lazy properties and with no-proxy.
public object Intercept(object target, string fieldName, object value)
{
+ // NH Specific: Hibernate only deals with lazy properties here, we deal with
+ // both lazy properties and with no-proxy.
if (initializing)
+ {
return InvokeImplementation;
+ }
- if (!IsUninitializedProperty(fieldName))
+ if (IsInitializedField(fieldName))
{
return value;
}
@@ -114,16 +112,20 @@
{
return InitializeField(fieldName, target);
}
-
- if (value.IsProxy() && unwrapProxyFieldNames != null && unwrapProxyFieldNames.Contains(fieldName))
+
+ if (value.IsProxy() && IsUninitializedAssociation(fieldName))
{
- var nhproxy = value as INHibernateProxy;
-
- return InitializeOrGetAssociation(nhproxy, fieldName);
+ var nhproxy = value as INHibernateProxy;
+ return InitializeOrGetAssociation(nhproxy, fieldName);
}
return InvokeImplementation;
}
+ private bool IsUninitializedAssociation(string fieldName)
+ {
+ return unwrapProxyFieldNames.Contains(fieldName) && !loadedUnwrapProxyFieldNames.Contains(fieldName);
+ }
+
private bool IsUninitializedProperty(string fieldName)
{
return uninitializedFields != null && uninitializedFields.Contains(fieldName);
Modified: trunk/nhibernate/src/NHibernate.Test/GhostProperty/GhostPropertyFixture.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/GhostProperty/GhostPropertyFixture.cs 2011-03-21 18:08:32 UTC (rev 5496)
+++ trunk/nhibernate/src/NHibernate.Test/GhostProperty/GhostPropertyFixture.cs 2011-03-21 20:09:18 UTC (rev 5497)
@@ -1,6 +1,8 @@
using System.Collections;
+using NHibernate.Cfg.Loquacious;
using NHibernate.Tuple.Entity;
using NUnit.Framework;
+using SharpTestsEx;
namespace NHibernate.Test.GhostProperty
{
@@ -19,6 +21,11 @@
get { return new[] { "GhostProperty.Mappings.hbm.xml" }; }
}
+ protected override void Configure(Cfg.Configuration configuration)
+ {
+ configuration.DataBaseIntegration(x=> x.LogFormatedSql = false);
+ }
+
protected override void OnSetUp()
{
using (var s = OpenSession())
@@ -109,6 +116,51 @@
}
}
+ [Test]
+ public void WillLoadGhostAssociationOnAccess()
+ {
+ // NH-2498
+ using (ISession s = OpenSession())
+ {
+ Order order;
+ using (var ls = new SqlLogSpy())
+ {
+ order = s.Get<Order>(1);
+ var logMessage = ls.GetWholeLog();
+ logMessage.Should().Not.Contain("FROM Payment");
+ }
+ order.Satisfy(o => !NHibernateUtil.IsPropertyInitialized(o, "Payment"));
+ // trigger on-access lazy load
+ var x = order.Payment;
+ order.Satisfy(o => NHibernateUtil.IsPropertyInitialized(o, "Payment"));
+ }
+ }
+
+ [Test]
+ public void WhenGetThenLoadOnlyNoLazyPlainProperties()
+ {
+ using (ISession s = OpenSession())
+ {
+ Order order;
+ using (var ls = new SqlLogSpy())
+ {
+ order = s.Get<Order>(1);
+ var logMessage = ls.GetWholeLog();
+ logMessage.Should().Not.Contain("ALazyProperty");
+ logMessage.Should().Contain("NoLazyProperty");
+ }
+ order.Satisfy(o => NHibernateUtil.IsPropertyInitialized(o, "NoLazyProperty"));
+ order.Satisfy(o => !NHibernateUtil.IsPropertyInitialized(o, "ALazyProperty"));
+
+ using (var ls = new SqlLogSpy())
+ {
+ var x = order.ALazyProperty;
+ var logMessage = ls.GetWholeLog();
+ logMessage.Should().Contain("ALazyProperty");
+ }
+ order.Satisfy(o => NHibernateUtil.IsPropertyInitialized(o, "ALazyProperty"));
+ }
+ }
}
}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate.Test/GhostProperty/Mappings.hbm.xml
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/GhostProperty/Mappings.hbm.xml 2011-03-21 18:08:32 UTC (rev 5496)
+++ trunk/nhibernate/src/NHibernate.Test/GhostProperty/Mappings.hbm.xml 2011-03-21 20:09:18 UTC (rev 5497)
@@ -8,9 +8,12 @@
<generator class="assigned" />
</id>
<many-to-one name="Payment" lazy="no-proxy"/>
- </class>
+ <property name="ALazyProperty" lazy="true"/>
+ <property name="NoLazyProperty"/>
+ </class>
+
<class name="Payment" abstract="true">
<id name="Id">
<generator class="assigned" />
Modified: trunk/nhibernate/src/NHibernate.Test/GhostProperty/Order.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/GhostProperty/Order.cs 2011-03-21 18:08:32 UTC (rev 5496)
+++ trunk/nhibernate/src/NHibernate.Test/GhostProperty/Order.cs 2011-03-21 20:09:18 UTC (rev 5497)
@@ -10,6 +10,9 @@
get { return payment; }
set { payment = value; }
}
+
+ public virtual string ALazyProperty { get; set; }
+ public virtual string NoLazyProperty { get; set; }
}
public abstract class Payment
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|