From e0f2b7ba5419b6e6f732da681a5960ddca185910 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Sun, 18 Mar 2018 19:59:40 +0530 Subject: [PATCH 1/2] Document "entity join" and "entity projection" --- doc/reference/modules/query_criteria.xml | 54 ++++++++++++++++++++++- doc/reference/modules/query_queryover.xml | 45 +++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/doc/reference/modules/query_criteria.xml b/doc/reference/modules/query_criteria.xml index 7339e3b6db4..46e490a11f9 100644 --- a/doc/reference/modules/query_criteria.xml +++ b/doc/reference/modules/query_criteria.xml @@ -141,12 +141,35 @@ var cats = sess.CreateCriteria() .List(); foreach ( IDictionary map in cats ) { - Cat cat = (Cat) map[CriteriaUtil.RootAlias]; + Cat cat = (Cat) map[CriteriaSpecification.RootAlias]; Cat kitten = (Cat) map["kt"]; }]]> + + Note that for retrieving just kittens you can also use entity projection. + See for more information. + - + + + Join entities without association (Entity joins or ad hoc joins) + + In criteria you have the ability to define a join to any entity, not just a mapped association. + To achieve it use CreateEntityAlias and CreateEntityCriteria. By example: + + + uniquelyNamedCats = sess.CreateCriteria("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();]]> + + Dynamic association fetching @@ -286,6 +309,33 @@ var results = session.CreateCriteria() .AddOrder( Order.Asc("kitName") ) .List();]]> + + You can also add entity projection to criteria: + + + () + .CreateCriteria("Kittens", "kt") + .Add(Expression.Eq("Name", "F%")) + .SetProjection(Projections.Entity(typeof(Cat), "kt")) + .List();]]> + + () + .CreateCriteria("Kittens", "kt") + .Add(Expression.Eq("Name", "F%")) + .SetProjection( + Projections.RootEntity(), + Projections.Entity(typeof(Cat), "kt")) + .List(); + +foreach (var objs in cats) +{ + Cat cat = (Cat) objs[0]; + Cat kitten = (Cat) objs[1]; +}]]> + + + See for more information. + diff --git a/doc/reference/modules/query_queryover.xml b/doc/reference/modules/query_queryover.xml index 9beab8431f5..f9230ca63c3 100644 --- a/doc/reference/modules/query_queryover.xml +++ b/doc/reference/modules/query_queryover.xml @@ -213,6 +213,25 @@ IList cats = + + Join entities without association (Entity joins or ad hoc joins) + + In QueryOver you have the ability to define a join to any entity, not just a mapped association. + To achieve it use JoinEntityAlias and JoinEntityQueryOver. By example: + + + (() => cat) + .JoinEntityAlias( + () => joinedCat, + () => cat.Name == joinedCat.Name && cat.Id != joinedCat.Id, + JoinType.LeftOuterJoin) + .Where(() => joinedCat.Id == null) + .List();]]> + + Aliases @@ -350,6 +369,32 @@ IList catReport = + + Entities Projection + + + You can add entity projections via AsEntity() extension. + + + () + .JoinAlias(c => c.Mate, () => mate) + .Select(c => c.AsEntity(), c => mate.Name) + .List();]]> + + + Or it can be done via Projections.RootEntity and Projections.Entity methods + if more control over loaded entities is required. For instance entity projections can be lazy loaded + or fetched with lazy properties: + + alias1).SetLazy(true), + Projections.Entity(() => alias2).SetFetchLazyProperties(true), + Projections.RootEntity() + )]]> + + Sub-queries From 8af2b5ef570faa00b274518a7790bde38d2cd553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= Date: Sun, 18 Mar 2018 16:11:07 +0100 Subject: [PATCH 2/2] Minor adjustments, to be squashed. --- doc/reference/modules/query_criteria.xml | 8 ++++---- doc/reference/modules/query_queryover.xml | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/doc/reference/modules/query_criteria.xml b/doc/reference/modules/query_criteria.xml index 46e490a11f9..e5533524197 100644 --- a/doc/reference/modules/query_criteria.xml +++ b/doc/reference/modules/query_criteria.xml @@ -145,7 +145,7 @@ foreach ( IDictionary map in cats ) Cat kitten = (Cat) map["kt"]; }]]> - Note that for retrieving just kittens you can also use entity projection. + Note that for retrieving just kittens you can also use an entity projection. See for more information. @@ -154,8 +154,8 @@ foreach ( IDictionary map in cats ) Join entities without association (Entity joins or ad hoc joins) - In criteria you have the ability to define a join to any entity, not just a mapped association. - To achieve it use CreateEntityAlias and CreateEntityCriteria. By example: + In criteria you have the ability to define a join to any entity, not just through a mapped association. + To achieve it, use CreateEntityAlias and CreateEntityCriteria. By example: uniquelyNamedCats = sess.CreateCriteria("c") @@ -310,7 +310,7 @@ var results = session.CreateCriteria() .List();]]> - You can also add entity projection to criteria: + You can also add an entity projection to a criteria query: () diff --git a/doc/reference/modules/query_queryover.xml b/doc/reference/modules/query_queryover.xml index f9230ca63c3..afaa2b0270f 100644 --- a/doc/reference/modules/query_queryover.xml +++ b/doc/reference/modules/query_queryover.xml @@ -216,8 +216,8 @@ IList cats = Join entities without association (Entity joins or ad hoc joins) - In QueryOver you have the ability to define a join to any entity, not just a mapped association. - To achieve it use JoinEntityAlias and JoinEntityQueryOver. By example: + In QueryOver you have the ability to define a join to any entity, not just through a mapped association. + To achieve it, use JoinEntityAlias and JoinEntityQueryOver. By example: catReport = Entities Projection - You can add entity projections via AsEntity() extension. + You can add entity projections via the AsEntity() extension. () .List();]]> - Or it can be done via Projections.RootEntity and Projections.Entity methods - if more control over loaded entities is required. For instance entity projections can be lazy loaded + Or it can be done via the Projections.RootEntity and Projections.Entity methods + if more control over loaded entities is required. For instance, entity projections can be lazy loaded or fetched with lazy properties: