Skip to content

Commit 1c19011

Browse files
committed
Fix
1 parent c6ad640 commit 1c19011

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/NHibernate/Engine/TableGroupJoinHelper.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ private static bool NeedsTableGroupJoin(IReadOnlyList<IJoin> joins, SqlString[]
6868

6969
foreach (var join in joins)
7070
{
71-
var entityPersister = GetEntityPersister(join.Joinable);
71+
var entityPersister = GetEntityPersister(join.Joinable, out var collection);
7272
if (entityPersister?.HasSubclassJoins(includeSubclasses && isSubclassIncluded(join.Alias)) != true)
7373
continue;
7474

7575
if (hasWithClause)
7676
return true;
7777

78-
if (entityPersister.ColumnsDependOnSubclassJoins(join.RHSColumns))
78+
if (collection?.IsManyToMany != true // many-to-many keys are stored in separate table
79+
&& entityPersister.ColumnsDependOnSubclassJoins(join.RHSColumns))
7980
return true;
8081
}
8182

@@ -91,14 +92,16 @@ private static SqlString GetTableGroupJoinWithClause(SqlString[] withClauseFragm
9192
var isAssociationJoin = lhsColumns.Length > 0;
9293
if (isAssociationJoin)
9394
{
94-
var entityPersister = GetEntityPersister(first.Joinable);
95+
var entityPersister = GetEntityPersister(first.Joinable, out var collection);
9596
string rhsAlias = first.Alias;
9697
string[] rhsColumns = first.RHSColumns;
9798
for (int j = 0; j < lhsColumns.Length; j++)
9899
{
99100
fromFragment.Add(lhsColumns[j])
100101
.Add("=")
101-
.Add(entityPersister?.GenerateTableAliasForColumn(rhsAlias, rhsColumns[j]) ?? rhsAlias)
102+
.Add(entityPersister == null || collection?.IsManyToMany == true // many-to-many keys are stored in separate table
103+
? rhsAlias
104+
: entityPersister.GenerateTableAliasForColumn(rhsAlias, rhsColumns[j]))
102105
.Add(".")
103106
.Add(rhsColumns[j]);
104107
if (j != lhsColumns.Length - 1)
@@ -111,12 +114,13 @@ private static SqlString GetTableGroupJoinWithClause(SqlString[] withClauseFragm
111114
return fromFragment.ToSqlString();
112115
}
113116

114-
private static AbstractEntityPersister GetEntityPersister(IJoinable joinable)
117+
private static AbstractEntityPersister GetEntityPersister(IJoinable joinable, out IQueryableCollection collection)
115118
{
119+
collection = null;
116120
if (!joinable.IsCollection)
117121
return joinable as AbstractEntityPersister;
118122

119-
var collection = (IQueryableCollection) joinable;
123+
collection = (IQueryableCollection) joinable;
120124
return collection.ElementType.IsEntityType ? collection.ElementPersister as AbstractEntityPersister : null;
121125
}
122126

0 commit comments

Comments
 (0)