8
8
using NHibernate . SqlCommand ;
9
9
using NHibernate . Type ;
10
10
using NHibernate . Util ;
11
+ using System . Linq ;
11
12
12
13
namespace NHibernate . Loader
13
14
{
@@ -158,7 +159,7 @@ private void AddAssociationToJoinTree(IAssociationType type, string[] aliasedLhs
158
159
OuterJoinableAssociation assoc =
159
160
new OuterJoinableAssociation ( type , alias , aliasedLhsColumns , subalias , joinType , GetWithClause ( path ) , Factory , enabledFilters ) ;
160
161
assoc . ValidateJoin ( path ) ;
161
- AddAssociation ( subalias , assoc ) ;
162
+ AddAssociation ( alias , assoc ) ;
162
163
163
164
int nextDepth = currentDepth + 1 ;
164
165
@@ -197,6 +198,7 @@ private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
197
198
{
198
199
var dependentField = dependentAlias . DependsOn [ j ] . ToLower ( ) ;
199
200
int end ;
201
+
200
202
if ( _indexes . TryGetValue ( dependentField , out end ) )
201
203
{
202
204
g . AddEdge ( i , end ) ;
@@ -211,25 +213,30 @@ private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
211
213
/// <summary>
212
214
/// Adds an association and extracts the aliases the association's 'with clause' is dependent on
213
215
/// </summary>
214
- private void AddAssociation ( string subalias , OuterJoinableAssociation association )
216
+ private void AddAssociation ( string lhsAlias , OuterJoinableAssociation association )
215
217
{
216
- subalias = subalias . ToLower ( ) ;
218
+ string rhsAlias = association . RHSAlias . ToLower ( ) ;
217
219
218
220
var dependencies = new List < string > ( ) ;
221
+
222
+ // the association always depends on the lhsAlias (note: the "On"-SqlString does not seem to include this part of the join SQL)
223
+ dependencies . Add ( lhsAlias . ToLower ( ) ) ;
224
+
225
+ // analyze the "On"-SqlString to find other aliases the association depends on.
219
226
var on = association . On . ToString ( ) ;
220
227
if ( ! String . IsNullOrEmpty ( on ) )
221
228
{
222
229
foreach ( Match match in aliasRegex . Matches ( on ) )
223
230
{
224
231
string alias = match . Groups [ 1 ] . Value ;
225
- if ( alias == subalias ) continue ;
232
+ if ( alias == rhsAlias ) continue ;
226
233
dependencies . Add ( alias . ToLower ( ) ) ;
227
234
}
228
235
}
229
236
230
237
_dependentAliases . Add ( new DependentAlias
231
238
{
232
- Alias = subalias ,
239
+ Alias = rhsAlias ,
233
240
DependsOn = dependencies . ToArray ( )
234
241
} ) ;
235
242
@@ -908,4 +915,4 @@ public string SelectString(IList<OuterJoinableAssociation> associations)
908
915
}
909
916
}
910
917
}
911
- }
918
+ }
0 commit comments