Skip to content

Commit 2d7097c

Browse files
committed
Expose Fetch method returning QueryOver
1 parent 8899395 commit 2d7097c

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/NHibernate.Test/Async/Criteria/SelectModeTest/SelectModeTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@ public async Task SelectModeJoinOnlyAsync()
5454
}
5555
}
5656

57+
[Test]
58+
public async Task SelectModeDetachedQueryOverAsync()
59+
{
60+
using (var sqlLog = new SqlLogSpy())
61+
using (var session = OpenSession())
62+
{
63+
EntityComplex root = null;
64+
root = await (QueryOver.Of(() => root)
65+
.Where(x => x.Id == _parentEntityComplexId)
66+
.Fetch(SelectMode.Fetch, r => r.Child1)
67+
.GetExecutableQueryOver(session)
68+
.SingleOrDefaultAsync());
69+
70+
Assert.That(root, Is.Not.Null);
71+
Assert.That(NHibernateUtil.IsInitialized(root), Is.True);
72+
Assert.That(root.Child1, Is.Not.Null);
73+
Assert.That(NHibernateUtil.IsInitialized(root.Child1), Is.True, "Joined ManyToOne Child1 should not be fetched.");
74+
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
75+
}
76+
}
77+
5778
[Test]
5879
public async Task SelectModeFetchAsync()
5980
{

src/NHibernate.Test/Criteria/SelectModeTest/SelectModeTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ public void SelectModeJoinOnly()
4343
}
4444
}
4545

46+
[Test]
47+
public void SelectModeDetachedQueryOver()
48+
{
49+
using (var sqlLog = new SqlLogSpy())
50+
using (var session = OpenSession())
51+
{
52+
EntityComplex root = null;
53+
root = QueryOver.Of(() => root)
54+
.Where(x => x.Id == _parentEntityComplexId)
55+
.Fetch(SelectMode.Fetch, r => r.Child1)
56+
.GetExecutableQueryOver(session)
57+
.SingleOrDefault();
58+
59+
Assert.That(root, Is.Not.Null);
60+
Assert.That(NHibernateUtil.IsInitialized(root), Is.True);
61+
Assert.That(root.Child1, Is.Not.Null);
62+
Assert.That(NHibernateUtil.IsInitialized(root.Child1), Is.True, "Joined ManyToOne Child1 should not be fetched.");
63+
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
64+
}
65+
}
66+
4667
[Test]
4768
public void SelectModeFetch()
4869
{

src/NHibernate/Criterion/QueryOver.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,12 @@ IQueryOverJoinBuilder<TRoot,TSubType> IQueryOver<TRoot,TSubType>.Right
10121012
IQueryOverJoinBuilder<TRoot,TSubType> IQueryOver<TRoot,TSubType>.Full
10131013
{ get { return new IQueryOverJoinBuilder<TRoot,TSubType>(this, JoinType.FullJoin); } }
10141014

1015-
public IQueryOver<TRoot, TSubType> Fetch(SelectMode mode, Expression<Func<TSubType, object>> path)
1015+
IQueryOver<TRoot, TSubType> ISupportSelectModeQueryOver<TRoot, TSubType>.Fetch(SelectMode mode, Expression<Func<TSubType, object>> path)
1016+
{
1017+
return Fetch(mode, path);
1018+
}
1019+
1020+
public QueryOver<TRoot, TSubType> Fetch(SelectMode mode, Expression<Func<TSubType, object>> path)
10161021
{
10171022
UnderlyingCriteria.Fetch(mode, ExpressionProcessor.FindMemberExpression(path.Body), null);
10181023
return this;

0 commit comments

Comments
 (0)