Skip to content

Full control of entities fetching in Criteria #1599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 23, 2018
10 changes: 5 additions & 5 deletions doc/reference/modules/performance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,20 @@ int accessLevel = permissions["accounts"]; // Error!]]></programlisting>
<literal>left join fetch</literal> in HQL. This tells NHibernate to fetch
the association eagerly in the first select, using an outer join. In the
<literal>ICriteria</literal> query API, you would use
<literal>SetFetchMode(FetchMode.Join)</literal>.
<literal>Fetch()</literal>.
</para>

<para>
If you ever feel like you wish you could change the fetching strategy used by
<literal>Get()</literal> or <literal>Load()</literal>, simply use a
<literal>ICriteria</literal> query, for example:
</para>

<programlisting><![CDATA[User user = session.CreateCriteria(typeof(User))
.SetFetchMode("Permissions", FetchMode.Join)
.Fetch(SelectMode.Fetch, "Permissions")
.Add( Expression.Eq("Id", userId) )
.UniqueResult<User>();]]></programlisting>

<para>
(This is NHibernate's equivalent of what some <emphasis>ORM</emphasis> solutions call a "fetch plan".)
</para>
Expand Down Expand Up @@ -475,7 +475,7 @@ using(var iter = session
<literal>NHibernateUtil.Initialize()</literal> for each collection that will
be needed in the web tier (this call must occur before the session is closed)
or retrieves the collection eagerly using a NHibernate query with a
<literal>FETCH</literal> clause or a <literal>FetchMode.Join</literal> in
<literal>FETCH</literal> clause or a <literal>SelectMode.Fetch</literal> in
<literal>ICriteria</literal>. This is usually easier if you adopt the
<emphasis>Command</emphasis> pattern instead of a <emphasis>Session Facade</emphasis>.
</para>
Expand Down
6 changes: 3 additions & 3 deletions doc/reference/modules/query_criteria.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ foreach ( IDictionary map in cats )

<para>
You may specify association fetching semantics at runtime using
<literal>SetFetchMode()</literal>.
<literal>Fetch()</literal>.
</para>

<programlisting><![CDATA[var cats = sess.CreateCriteria<Cat>()
.Add( Expression.Like("Name", "Fritz%") )
.SetFetchMode("Mate", FetchMode.Eager)
.SetFetchMode("Kittens", FetchMode.Eager)
.Fetch(SelectMode.Fetch, "Mate")
.Fetch(SelectMode.Fetch, "Kittens")
.List<Cat>();]]></programlisting>

<para>
Expand Down
8 changes: 4 additions & 4 deletions src/NHibernate.Test/Async/Criteria/CriteriaQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ public async Task ProjectionsTestAsync()

object g = await (s.CreateCriteria(typeof(Student))
.Add(Expression.IdEq(667L))
.SetFetchMode("enrolments", FetchMode.Join)
.Fetch("enrolments")
//.setFetchMode("enrolments.course", FetchMode.JOIN) //TODO: would love to make that work...
.UniqueResultAsync());
Assert.AreSame(gavin, g);
Expand Down Expand Up @@ -1279,7 +1279,7 @@ public async Task CloningProjectionsTestAsync()

ICriteria criteriaToClone6 = s.CreateCriteria(typeof(Student))
.Add(Expression.IdEq(667L))
.SetFetchMode("enrolments", FetchMode.Join);
.Fetch("enrolments");
object g = await (CriteriaTransformer.Clone(criteriaToClone6)
.UniqueResultAsync());
Assert.AreSame(gavin, g);
Expand Down Expand Up @@ -2426,7 +2426,7 @@ public async Task SubcriteriaJoinTypesAsync()
}

result = await (session.CreateCriteria(typeof(Student))
.SetFetchMode("PreferredCourse", FetchMode.Join)
.Fetch("PreferredCourse")
.CreateCriteria("PreferredCourse", JoinType.LeftOuterJoin)
.AddOrder(Order.Asc("CourseCode"))
.ListAsync());
Expand All @@ -2436,7 +2436,7 @@ public async Task SubcriteriaJoinTypesAsync()
Assert.IsNotNull(result[2]);

result = await (session.CreateCriteria(typeof(Student))
.SetFetchMode("PreferredCourse", FetchMode.Join)
.Fetch("PreferredCourse")
.CreateAlias("PreferredCourse", "pc", JoinType.LeftOuterJoin)
.AddOrder(Order.Asc("pc.CourseCode"))
.ListAsync());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public async Task OverrideEagerJoinAsync()
{
var persons =
await (s.QueryOver<Parent>()
.Fetch(p => p.Children).Lazy
.Fetch(SelectMode.Skip, p => p.Children)
.ListAsync());

Assert.That(persons.Count, Is.EqualTo(1));
Expand Down
Loading