Skip to content

Commit c436dd4

Browse files
committed
Fix Criteria withClause for many-to-many
1 parent bc3f56b commit c436dd4

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

src/NHibernate.Test/Async/NHSpecificTest/GH1994/Fixture.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010

1111
using System.Linq;
12+
using NHibernate.Criterion;
1213
using NHibernate.Dialect;
1314
using NHibernate.Linq;
15+
using NHibernate.SqlCommand;
1416
using NHibernate.Transform;
1517
using NUnit.Framework;
1618

@@ -82,6 +84,7 @@ public async Task TestFilteredByWhereCollectionLinqQueryAsync()
8284
}
8385
}
8486

87+
//GH-1994
8588
[Test]
8689
public async Task TestFilteredLinqQueryAsync()
8790
{
@@ -113,5 +116,22 @@ public async Task TestFilteredQueryOverAsync()
113116
Assert.That(query[0].Documents.Count, Is.EqualTo(1), "filtered asset documents");
114117
}
115118
}
119+
120+
//NH-2991
121+
[Test]
122+
public async Task TestQueryOverRestrictionWithClauseAsync()
123+
{
124+
using (var s = OpenSession())
125+
{
126+
Document docs = null;
127+
var query = await (s.QueryOver<Asset>()
128+
.JoinQueryOver(a => a.Documents, () => docs, JoinType.LeftOuterJoin, Restrictions.Where(() => docs.IsDeleted != true))
129+
.TransformUsing(Transformers.DistinctRootEntity)
130+
.ListAsync<Asset>());
131+
132+
Assert.That(query.Count, Is.EqualTo(1), "filtered assets");
133+
Assert.That(query[0].Documents.Count, Is.EqualTo(1), "filtered asset documents");
134+
}
135+
}
116136
}
117137
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Linq;
2+
using NHibernate.Criterion;
23
using NHibernate.Dialect;
34
using NHibernate.Linq;
5+
using NHibernate.SqlCommand;
46
using NHibernate.Transform;
57
using NUnit.Framework;
68

@@ -71,6 +73,7 @@ public void TestFilteredByWhereCollectionLinqQuery()
7173
}
7274
}
7375

76+
//GH-1994
7477
[Test]
7578
public void TestFilteredLinqQuery()
7679
{
@@ -102,5 +105,22 @@ public void TestFilteredQueryOver()
102105
Assert.That(query[0].Documents.Count, Is.EqualTo(1), "filtered asset documents");
103106
}
104107
}
108+
109+
//NH-2991
110+
[Test]
111+
public void TestQueryOverRestrictionWithClause()
112+
{
113+
using (var s = OpenSession())
114+
{
115+
Document docs = null;
116+
var query = s.QueryOver<Asset>()
117+
.JoinQueryOver(a => a.Documents, () => docs, JoinType.LeftOuterJoin, Restrictions.Where(() => docs.IsDeleted != true))
118+
.TransformUsing(Transformers.DistinctRootEntity)
119+
.List<Asset>();
120+
121+
Assert.That(query.Count, Is.EqualTo(1), "filtered assets");
122+
Assert.That(query[0].Documents.Count, Is.EqualTo(1), "filtered asset documents");
123+
}
124+
}
105125
}
106126
}

src/NHibernate/Loader/JoinWalker.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ private void AddAssociationToJoinTree(IAssociationType type, string[] aliasedLhs
168168
IJoinable joinable = type.GetAssociatedJoinable(Factory);
169169

170170
string subalias = GenerateTableAlias(associations.Count + 1, path, subPathAlias, joinable);
171+
var qc = joinable.IsCollection ? (IQueryableCollection) joinable : null;
171172

172173
var assoc =
173174
new OuterJoinableAssociation(
@@ -176,7 +177,8 @@ private void AddAssociationToJoinTree(IAssociationType type, string[] aliasedLhs
176177
aliasedLhsColumns,
177178
subalias,
178179
joinType,
179-
GetWithClause(path, subPathAlias),
180+
//for many-to-many with clause is applied with OuterJoinableAssociation created for entity persister so simply skip it here
181+
qc?.IsManyToMany == true ? null : GetWithClause(path, subPathAlias),
180182
Factory,
181183
enabledFilters,
182184
GetSelectMode(path));
@@ -185,17 +187,15 @@ private void AddAssociationToJoinTree(IAssociationType type, string[] aliasedLhs
185187

186188
int nextDepth = currentDepth + 1;
187189

188-
if (!joinable.IsCollection)
190+
if (qc == null)
189191
{
190192
IOuterJoinLoadable pjl = joinable as IOuterJoinLoadable;
191193
if (pjl != null)
192194
WalkEntityTree(pjl, subalias, path, nextDepth);
193195
}
194196
else
195197
{
196-
IQueryableCollection qc = joinable as IQueryableCollection;
197-
if (qc != null)
198-
WalkCollectionTree(qc, subalias, path, subPathAlias, nextDepth);
198+
WalkCollectionTree(qc, subalias, path, subPathAlias, nextDepth);
199199
}
200200
}
201201

0 commit comments

Comments
 (0)