2
2
using System . Collections . Generic ;
3
3
using Iesi . Collections . Generic ;
4
4
using NHibernate . Engine ;
5
+ using NHibernate . Persister . Entity ;
5
6
using NHibernate . Proxy ;
6
7
using NHibernate . Util ;
7
8
@@ -113,16 +114,27 @@ public object Intercept(object target, string fieldName, object value, bool sett
113
114
uninitializedFields . Remove ( fieldName ) ;
114
115
}
115
116
116
- if ( IsUninitializedAssociation ( fieldName ) )
117
+ if ( ! unwrapProxyFieldNames . Contains ( fieldName ) )
118
+ {
119
+ return value ;
120
+ }
121
+
122
+ if ( ! value . IsProxy ( ) || NHibernateUtil . IsInitialized ( value ) )
117
123
{
118
124
loadedUnwrapProxyFieldNames . Add ( fieldName ) ;
119
125
}
126
+ else
127
+ {
128
+ loadedUnwrapProxyFieldNames . Remove ( fieldName ) ;
129
+ }
130
+
131
+ return value ;
120
132
}
121
133
122
134
if ( IsInitializedField ( fieldName ) )
123
135
{
124
136
if ( value . IsProxy ( ) && IsInitializedAssociation ( fieldName ) )
125
- return InitializeOrGetAssociation ( ( INHibernateProxy ) value , fieldName ) ;
137
+ return InitializeOrGetAssociation ( target , ( INHibernateProxy ) value , fieldName ) ;
126
138
127
139
return value ;
128
140
}
@@ -144,7 +156,7 @@ public object Intercept(object target, string fieldName, object value, bool sett
144
156
if ( value . IsProxy ( ) && IsUninitializedAssociation ( fieldName ) )
145
157
{
146
158
var nhproxy = value as INHibernateProxy ;
147
- return InitializeOrGetAssociation ( nhproxy , fieldName ) ;
159
+ return InitializeOrGetAssociation ( target , nhproxy , fieldName ) ;
148
160
}
149
161
return InvokeImplementation ;
150
162
}
@@ -164,13 +176,19 @@ private bool IsUninitializedProperty(string fieldName)
164
176
return uninitializedFields != null && uninitializedFields . Contains ( fieldName ) ;
165
177
}
166
178
167
- private object InitializeOrGetAssociation ( INHibernateProxy value , string fieldName )
179
+ private object InitializeOrGetAssociation ( object target , INHibernateProxy value , string fieldName )
168
180
{
169
181
if ( value . HibernateLazyInitializer . IsUninitialized )
170
182
{
171
183
value . HibernateLazyInitializer . Initialize ( ) ;
172
184
value . HibernateLazyInitializer . Unwrap = true ; // means that future Load/Get from the session will get the implementation
173
185
loadedUnwrapProxyFieldNames . Add ( fieldName ) ;
186
+ // Set the property value in order to be accessible when the session is closed
187
+ var implValue = value . HibernateLazyInitializer . GetImplementation ( session ) ;
188
+ var persister = session . Factory . GetEntityPersister ( entityName ) ;
189
+ persister . SetPropertyValue ( target , persister . GetUnwrapProxyPropertyIndex ( fieldName ) , implValue ) ;
190
+
191
+ return implValue ;
174
192
}
175
193
return value . HibernateLazyInitializer . GetImplementation ( session ) ;
176
194
}
0 commit comments