Skip to content

Commit e0f2b7b

Browse files
committed
Document "entity join" and "entity projection"
1 parent 2f703d0 commit e0f2b7b

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

doc/reference/modules/query_criteria.xml

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,35 @@ var cats = sess.CreateCriteria<Cat>()
141141
.List<IDictionary>();
142142
foreach ( IDictionary map in cats )
143143
{
144-
Cat cat = (Cat) map[CriteriaUtil.RootAlias];
144+
Cat cat = (Cat) map[CriteriaSpecification.RootAlias];
145145
Cat kitten = (Cat) map["kt"];
146146
}]]></programlisting>
147+
<para>
148+
Note that for retrieving just kittens you can also use entity projection.
149+
See <xref linkend="querycriteria-projection"/> for more information.
150+
</para>
147151

148152
</sect1>
149-
153+
154+
<sect1 id="querycriteria_entityjoin">
155+
<title>Join entities without association (Entity joins or ad hoc joins)</title>
156+
<para>
157+
In criteria you have the ability to define a join to any entity, not just a mapped association.
158+
To achieve it use <literal>CreateEntityAlias</literal> and <literal>CreateEntityCriteria</literal>. By example:
159+
</para>
160+
161+
<programlisting><![CDATA[IList<Cat> uniquelyNamedCats = sess.CreateCriteria<Cat>("c")
162+
.CreateEntityAlias(
163+
"joinedCat",
164+
Restrictions.And(
165+
Restrictions.EqProperty("c.Name", "joinedCat.Name"),
166+
Restrictions.NotEqProperty("c.Id", "joinedCat.Id")),
167+
JoinType.LeftOuterJoin,
168+
typeof(Cat).FullName)
169+
.Add(Restrictions.IsNull("joinedCat.Id"))
170+
.List();]]></programlisting>
171+
</sect1>
172+
150173
<sect1 id="querycriteria-dynamicfetching">
151174
<title>Dynamic association fetching</title>
152175

@@ -286,6 +309,33 @@ var results = session.CreateCriteria<Cat>()
286309
.AddOrder( Order.Asc("kitName") )
287310
.List<object[]>();]]></programlisting>
288311

312+
<para>
313+
You can also add entity projection to criteria:
314+
</para>
315+
316+
<programlisting><![CDATA[var kittens = sess.CreateCriteria<Cat>()
317+
.CreateCriteria("Kittens", "kt")
318+
.Add(Expression.Eq("Name", "F%"))
319+
.SetProjection(Projections.Entity(typeof(Cat), "kt"))
320+
.List();]]></programlisting>
321+
322+
<programlisting><![CDATA[var cats = sess.CreateCriteria<Cat>()
323+
.CreateCriteria("Kittens", "kt")
324+
.Add(Expression.Eq("Name", "F%"))
325+
.SetProjection(
326+
Projections.RootEntity(),
327+
Projections.Entity(typeof(Cat), "kt"))
328+
.List<object[]>();
329+
330+
foreach (var objs in cats)
331+
{
332+
Cat cat = (Cat) objs[0];
333+
Cat kitten = (Cat) objs[1];
334+
}]]></programlisting>
335+
336+
<para>
337+
See <xref linkend="queryqueryover-projectionentities"/> for more information.
338+
</para>
289339
</sect1>
290340

291341
<sect1 id="querycriteria-detachedqueries">

doc/reference/modules/query_queryover.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,25 @@ IList<Cat> cats =
213213

214214
</sect1>
215215

216+
<sect1 id="queryqueryover_entityjoin">
217+
<title>Join entities without association (Entity joins or ad hoc joins)</title>
218+
<para>
219+
In QueryOver you have the ability to define a join to any entity, not just a mapped association.
220+
To achieve it use <literal>JoinEntityAlias</literal> and <literal>JoinEntityQueryOver</literal>. By example:
221+
</para>
222+
223+
<programlisting><![CDATA[Cat cat = null;
224+
Cat joinedCat = null;
225+
226+
var uniquelyNamedCats = sess.QueryOver<Cat>(() => cat)
227+
.JoinEntityAlias(
228+
() => joinedCat,
229+
() => cat.Name == joinedCat.Name && cat.Id != joinedCat.Id,
230+
JoinType.LeftOuterJoin)
231+
.Where(() => joinedCat.Id == null)
232+
.List();]]></programlisting>
233+
</sect1>
234+
216235
<sect1 id="queryqueryover-aliases">
217236
<title>Aliases</title>
218237

@@ -350,6 +369,32 @@ IList<CatSummary> catReport =
350369

351370
</sect1>
352371

372+
<sect1 id="queryqueryover-projectionentities">
373+
<title>Entities Projection</title>
374+
375+
<para>
376+
You can add entity projections via <literal>AsEntity()</literal> extension.
377+
</para>
378+
379+
<programlisting><![CDATA[Cat mate = null;
380+
381+
var catAndMateNameList = sess.QueryOver<Cat>()
382+
.JoinAlias(c => c.Mate, () => mate)
383+
.Select(c => c.AsEntity(), c => mate.Name)
384+
.List<object[]>();]]></programlisting>
385+
386+
<para>
387+
Or it can be done via <literal>Projections.RootEntity</literal> and <literal>Projections.Entity</literal> methods
388+
if more control over loaded entities is required. For instance entity projections can be lazy loaded
389+
or fetched with lazy properties:
390+
</para>
391+
<programlisting><![CDATA[.Select(
392+
Projections.Entity(() => alias1).SetLazy(true),
393+
Projections.Entity(() => alias2).SetFetchLazyProperties(true),
394+
Projections.RootEntity()
395+
)]]></programlisting>
396+
</sect1>
397+
353398
<sect1 id="queryqueryover-subqueries">
354399
<title>Sub-queries</title>
355400

0 commit comments

Comments
 (0)