title: NHibernate criteria query help
body: <p>Given the following tables, I am trying to return all Allocations for a given Resource's that fall between a given range of dates using a criteria query:</p>
  
  <pre><code>create table Resources (
      ResourceId  integer,
     ResourceName TEXT not null,
     BusinessId TEXT not null,
     OrganizationName TEXT not null,
     primary key (ResourceId)
  )
  
  create table Allocations (
      AllocationId  integer,
     StartTime DATETIME not null,
     EndTime DATETIME not null,
     PostingTime DATETIME,
     ResourceId INTEGER,
     ActivityBaseId INTEGER,
     primary key (AllocationId),
    unique (StartTime, EndTime, ResourceId, ActivityBaseId)
  )
  
  public Resource FetchFor(Resource resource, DateRange range) {
      var allocations = _session.CreateCriteria&lt;Resource&gt;()
              .CreateCriteria("Allocations")
                  .Add(Restrictions.Between("EndTime", (DateTime)range.Start, (DateTime)range.End))
              .List&lt;Allocation&gt;();
  
          if (allocations.Count() &gt; 0) 
              resource.ReplaceAllocations(allocations);
  
          return resource;
      }
  </code></pre>
  
  <p>I get this exception when I run this: </p>
  
  <pre><code>failed: NHibernate.QueryException : could not resolve property: EndTime of: Domain.Model.Allocations.Allocation
  </code></pre>
  
  <p>The model is mapped and I can save a Resource and it's associated Allocations without trouble, as I am doing to test this Fetch query. An example line of NHib sql:
      NHibernate: INSERT INTO Allocations (StartTime, EndTime, PostingTime, ResourceId, ActivityBaseId) VALUES (@p0, @p1, @p2, @p3, @p4); select last_insert_rowid();@p0 = 1/28/2010 12:00:00 AM, @p1 = 1/28/2010 1:00:00 AM, @p2 = NULL, @p3 = 1, @p4 = 4</p>
  
  <p>The exception message is confusing since NHib does clearly understand how to map Allocation.EndTime. Please:<br>
  1) help trouble shoot / clean up the query in this post, and<br>
  2) point out any favorite resources for learning nhib queries, especially if there are examples there.</p>
  
  <p>As an aside I do realize I am not using the Resource param in the example shown, as I pretty much (mis?)applied the code from an example on Ayende's <a href="http://ayende.com/Blog/archive/2009/05/19/nhibernate-queries-examples.aspx" rel="nofollow noreferrer">posting</a>. If I can get this join query working I will turn it into a subquery for performance.</p>
  
  <p>Thanks!<br>
  Berryl</p>
  
  <p>=== NHib Mapping =====</p>
  
  <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property"   auto-import="true" default-cascade="none" default-lazy="true"&gt;
  &lt;class xmlns="urn:nhibernate-mapping-2.2" name="Domain.Model.Allocations.Allocation, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Allocations"&gt;
  &lt;id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0"&gt;
    &lt;column name="AllocationId" /&gt;
    &lt;generator class="identity" /&gt;
  &lt;/id&gt;
  &lt;property name="TimeRange" type="Data.UserTypes.AllocationTimeRangeUserType, Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"&gt;
    &lt;column name="StartTime" not-null="true" unique-key="DomainSignature" /&gt;
    &lt;column name="EndTime" not-null="true" unique-key="DomainSignature" /&gt;
  &lt;/property&gt;
  &lt;property name="PostingTime" type="System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
    &lt;column name="PostingTime" not-null="false" /&gt;
  &lt;/property&gt;
  &lt;many-to-one class="Domain.Model.Resources.Resource, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" foreign-key="Resource_FK" name="Resource"&gt;
    &lt;column name="ResourceId" unique-key="DomainSignature" /&gt;
  &lt;/many-to-one&gt;
  &lt;many-to-one class="Domain.Model.Activities.ActivityBase, Smack.ConstructionAdmin.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" foreign-key="ActivityBase_FK" name="Activity"&gt;
    &lt;column name="ActivityBaseId" unique-key="DomainSignature" /&gt;
  &lt;/many-to-one&gt;
  </code></pre>
  
  <p>
  </p>