title: Different results using IQuery and Repository in Web App
body: <p>Hope someone can point me in the right direction.  I'm just starting out with nHibernate and a little confused over this one.  It's running within a .Net Web Application.</p>
  
  <p>Basically I've got 2 classes - Coupon and Publisher.</p>
  
  <p>As a test, and to make sure NH was set up correctly, I accessed the PublisherRepository and pulled a publisher out by it's name.  That works fine and reports success.</p>
  
  <pre><code>IPublisherRepository repo = new PublisherRepository();
  Response.Write(repo.GetByName("Publisher 5"));
  </code></pre>
  
  <p>As a second test, I then used the CreateQuery method to get all of the Publishers, like so:</p>
  
  <pre><code>IQuery query = session.CreateQuery("from CartManData.Domain.Publisher pub");
  </code></pre>
  
  <p>This returns no data - the list is empty.  Same goes using Linq:</p>
  
  <pre><code>session.Query&lt;Publisher&gt;().Where(x=&gt;x.Name == "Publisher 4").ToList&lt;Publisher&gt;()
  </code></pre>
  
  <p>Using Sql Profiler I can see that the first test hits the database, and retrieves the set (called Coupons belonging to a Publisher) as lazy loading is off.  However, the second 2 approaches don't hit the database at all - and I'm stumped as to why.</p>
  
  <p>Here's the mapping file for Publisher and Coupon.  They're embedded and I know they're working otherwise the repo wouldn't work either :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                     assembly="CartManData"
                     namespace="CartManData.Domain"&gt;
  
    &lt;class name="Publisher" lazy="false"&gt;
      &lt;id name="Id"&gt;
        &lt;generator class="guid"&gt;&lt;/generator&gt;
      &lt;/id&gt;
      &lt;property name="Name"&gt;&lt;/property&gt;
      &lt;property name="AddressLine1"&gt;&lt;/property&gt;
      &lt;property name="AddressLine2"&gt;&lt;/property&gt;
      &lt;property name="AddressLine3"&gt;&lt;/property&gt;
      &lt;property name="Town"&gt;&lt;/property&gt;
      &lt;property name="PostCode"&gt;&lt;/property&gt;
      &lt;property name="Telephone"&gt;&lt;/property&gt;
      &lt;property name="Email"&gt;&lt;/property&gt;
      &lt;property name="Enabled"&gt;&lt;/property&gt;
      &lt;property name="CommissionRate"&gt;&lt;/property&gt;
      &lt;set name="Coupons" cascade="none" lazy="false"&gt;
        &lt;key column="PublisherId" &gt;&lt;/key&gt;
        &lt;one-to-many class="Coupon" /&gt;
      &lt;/set&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>And Coupons:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                     assembly="CartManData"
                     namespace="CartManData.Domain"&gt;
  
    &lt;class name="Coupon"&gt;
      &lt;id name="Id"&gt;
        &lt;generator class="guid"&gt;&lt;/generator&gt;
      &lt;/id&gt;
      &lt;property name="Name"&gt;&lt;/property&gt;
      &lt;property name="EffectiveFrom"&gt;&lt;/property&gt;
      &lt;property name="EffectiveTo"&gt;&lt;/property&gt;
      &lt;property name="UnitPrice"&gt;&lt;/property&gt;
      &lt;property name="OriginalPrice"&gt;&lt;/property&gt;
      &lt;property name="CouponImage"&gt;&lt;/property&gt;
      &lt;property name="Enabled"&gt;&lt;/property&gt;
      &lt;property name="PublisherId" not-null="false"&gt;&lt;/property&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Any help on this really appreciate - sure it's something I've missed.</p>
  
  <p>Cheers,
  Tony</p>
  
  <p><strong>Additional Info</strong></p>
  
  <p>The Session object is retrieved via a HttpModule, where the session object is bound to CurrentSessionContext.  That seems to be working fine as if you check to see if session is open, it reports it is.</p>
  
  <p>PublisherRepository.GetByName() looks like this:</p>
  
  <pre><code>using (ISession session = NHibernateHelper.OpenSession())
              {
                  return session.CreateCriteria(typeof(Publisher))
                      .Add(NHibernate.Criterion.Restrictions.Eq("Name", name))
                      .UniqueResult&lt;Publisher&gt;();
              }
  </code></pre>
  
  <p><strong>Log4Net Output</strong></p>
  
  <p>During the call via CreateQuery (the 2nd example above), this is what NHibernate is reporting:</p>
  
  <pre><code>2012-08-22 16:22:28,075 [15] DEBUG rollingFile - START of retrieval
  2012-08-22 16:22:28,081 [15] DEBUG NHibernate.Engine.Query.QueryPlanCache - unable to locate HQL query plan in cache; generating (from CartManData.Domain.Publisher pub)
  2012-08-22 16:22:28,128 [15] DEBUG NHibernate.Hql.Ast.ANTLR.HqlParseEngine - parse() - HQL: from CartManData.Domain.Publisher pub
  2012-08-22 16:22:28,174 [15] DEBUG NHibernate.Hql.Ast.ANTLR.ErrorCounter - throwQueryException() : no errors
  2012-08-22 16:22:28,200 [15] DEBUG NHibernate.Engine.Query.QueryPlanCache - unable to locate HQL query plan in cache; generating (from CartManData.Domain.Publisher pub)
  2012-08-22 16:22:28,201 [15] DEBUG NHibernate.Hql.Ast.ANTLR.HqlParseEngine - parse() - HQL: from CartManData.Domain.Publisher pub
  2012-08-22 16:22:28,202 [15] DEBUG NHibernate.Hql.Ast.ANTLR.ErrorCounter - throwQueryException() : no errors
  2012-08-22 16:22:28,206 [15] DEBUG NHibernate.Engine.Query.HQLQueryPlan - enumerable: from CartManData.Domain.Publisher pub
  2012-08-22 16:22:28,208 [15] DEBUG NHibernate.Engine.QueryParameters - named parameters: {}
  2012-08-22 16:22:28,210 [15] DEBUG rollingFile - End of retrieval
  </code></pre>