Skip to content

Document "entity join" and "entity projection" #1619

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 2 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions doc/reference/modules/query_criteria.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,35 @@ var cats = sess.CreateCriteria<Cat>()
.List<IDictionary>();
foreach ( IDictionary map in cats )
{
Cat cat = (Cat) map[CriteriaUtil.RootAlias];
Cat cat = (Cat) map[CriteriaSpecification.RootAlias];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So NH-2122 has missed this one, good spot.

Cat kitten = (Cat) map["kt"];
}]]></programlisting>
<para>
Note that for retrieving just kittens you can also use an entity projection.
See <xref linkend="querycriteria-projection"/> for more information.
</para>

</sect1>


<sect1 id="querycriteria_entityjoin">
<title>Join entities without association (Entity joins or ad hoc joins)</title>
<para>
In criteria you have the ability to define a join to any entity, not just through a mapped association.
To achieve it, use <literal>CreateEntityAlias</literal> and <literal>CreateEntityCriteria</literal>. By example:
</para>

<programlisting><![CDATA[IList<Cat> uniquelyNamedCats = sess.CreateCriteria<Cat>("c")
.CreateEntityAlias(
"joinedCat",
Restrictions.And(
Restrictions.EqProperty("c.Name", "joinedCat.Name"),
Restrictions.NotEqProperty("c.Id", "joinedCat.Id")),
JoinType.LeftOuterJoin,
typeof(Cat).FullName)
.Add(Restrictions.IsNull("joinedCat.Id"))
.List();]]></programlisting>
</sect1>

<sect1 id="querycriteria-dynamicfetching">
<title>Dynamic association fetching</title>

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

<para>
You can also add an entity projection to a criteria query:
</para>

<programlisting><![CDATA[var kittens = sess.CreateCriteria<Cat>()
.CreateCriteria("Kittens", "kt")
.Add(Expression.Eq("Name", "F%"))
.SetProjection(Projections.Entity(typeof(Cat), "kt"))
.List();]]></programlisting>

<programlisting><![CDATA[var cats = sess.CreateCriteria<Cat>()
.CreateCriteria("Kittens", "kt")
.Add(Expression.Eq("Name", "F%"))
.SetProjection(
Projections.RootEntity(),
Projections.Entity(typeof(Cat), "kt"))
.List<object[]>();

foreach (var objs in cats)
{
Cat cat = (Cat) objs[0];
Cat kitten = (Cat) objs[1];
}]]></programlisting>

<para>
See <xref linkend="queryqueryover-projectionentities"/> for more information.
</para>
</sect1>

<sect1 id="querycriteria-detachedqueries">
Expand Down
45 changes: 45 additions & 0 deletions doc/reference/modules/query_queryover.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@ IList<Cat> cats =

</sect1>

<sect1 id="queryqueryover_entityjoin">
<title>Join entities without association (Entity joins or ad hoc joins)</title>
<para>
In QueryOver you have the ability to define a join to any entity, not just through a mapped association.
To achieve it, use <literal>JoinEntityAlias</literal> and <literal>JoinEntityQueryOver</literal>. By example:
</para>

<programlisting><![CDATA[Cat cat = null;
Cat joinedCat = null;

var uniquelyNamedCats = sess.QueryOver<Cat>(() => cat)
.JoinEntityAlias(
() => joinedCat,
() => cat.Name == joinedCat.Name && cat.Id != joinedCat.Id,
JoinType.LeftOuterJoin)
.Where(() => joinedCat.Id == null)
.List();]]></programlisting>
</sect1>

<sect1 id="queryqueryover-aliases">
<title>Aliases</title>

Expand Down Expand Up @@ -350,6 +369,32 @@ IList<CatSummary> catReport =

</sect1>

<sect1 id="queryqueryover-projectionentities">
<title>Entities Projection</title>

<para>
You can add entity projections via the <literal>AsEntity()</literal> extension.
</para>

<programlisting><![CDATA[Cat mate = null;

var catAndMateNameList = sess.QueryOver<Cat>()
.JoinAlias(c => c.Mate, () => mate)
.Select(c => c.AsEntity(), c => mate.Name)
.List<object[]>();]]></programlisting>

<para>
Or it can be done via the <literal>Projections.RootEntity</literal> and <literal>Projections.Entity</literal> methods
if more control over loaded entities is required. For instance, entity projections can be lazy loaded
or fetched with lazy properties:
</para>
<programlisting><![CDATA[.Select(
Projections.Entity(() => alias1).SetLazy(true),
Projections.Entity(() => alias2).SetFetchLazyProperties(true),
Projections.RootEntity()
)]]></programlisting>
</sect1>

<sect1 id="queryqueryover-subqueries">
<title>Sub-queries</title>

Expand Down