Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To explain the difference between CreateCriteria and CreateAlias in NHibernate 2.0 + lets see the following domain model.</p> <pre><code>public class Product { public virtual int Id { get; private set; } public virtual string Name { get; set; } public virtual decimal Price { get; set; } public virtual Category Category { get; set; } public virtual IList&lt;ProductStock&gt; ProductStocks { get; set; } } public class Category { public virtual int Id { get; private set; } public virtual string Name { get; set; } public virtual IList&lt;Product&gt; Products { get; set; } } public class ProductStock { public virtual int Id { get; private set; } public virtual Product Product { get; set; } public virtual string WarehouseName { get; set; } public virtual int Stock { get; set; } } </code></pre> <p>Now if you write following criteria to inner join these entities</p> <pre><code>var criteria = DetachedCriteria.For&lt;Product&gt;() .CreateCriteria("Category", JoinType.InnerJoin) .CreateCriteria("ProductStocks", "ps", JoinType.InnerJoin) .Add(Restrictions.Le("ps.Stock",10)); </code></pre> <p>The above criteria wont work because when the first CreateCriteria runs it return "Category" entity, therefore when the second CreateCriteria execute it wont find property ProductStocks in the "Category" entity and the query will fail.</p> <p>So the correct way to write this criteria is</p> <pre><code>var criteria = DetachedCriteria.For&lt;Product&gt;() .CreateAlias("Category", "c", JoinType.InnerJoin) .CreateCriteria("ProductStocks", "ps", JoinType.InnerJoin) .Add(Restrictions.Le("ps.Stock",10)); </code></pre> <p>When the first CreateAlias runs it return "Product" entity, when the second CreateCriteria execute it will find property ProductStocks in the "Product" entity.</p> <p>So the TSQL will be like this.</p> <pre><code>SELECT this_.ProductID as ProductID8_2_, this_.Name as Name8_2_, this_.Price as Price8_2_, this_.CategoryID as CategoryID8_2_, ps2_.ProductStockID as ProductS1_9_0_, ps2_.Stock as Stock9_0_, ps2_.ProductID as ProductID9_0_, ps2_.WarehouseID as Warehous4_9_0_, c1_.CategoryID as CategoryID0_1_, c1_.Name as Name0_1_ FROM [Product] this_ inner join [ProductStock] ps2_ on this_.ProductID = ps2_.ProductID inner join [Category] c1_ on this_.CategoryID = c1_.CategoryID WHERE ps2_.Stock &lt;= 10 </code></pre> <p>I hope this will help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload