title: NHibernate Criteria: Add a constraint for property type
body: <p>I have a program with a base class (Message) from which many other classes derivate (notably Article and Comment). I would like to create a criteria that will select all the Comments for which the RootMessage property is of type Article (I have some other type of potential RootMessages). But I can't seem to find how to add that kind of constraint in a criteria.</p>
  
  <p>For now my code is this:</p>
  
  <pre><code> public virtual ICollection&lt;Comment&gt; GetCommentsByArticle(ISession session)
      {
          var c = session.CreateCriteria(typeof(Comment))
              .Add(Expression.Eq("Author", this))
              .AddOrder(Order.Desc("CreationDate"))
              .SetMaxResults(20);
          var commentList = c.List&lt;Comment&gt;();
          return commentList;
  
      }
  </code></pre>
  
  <p>But of course this returns every single comment of this Author. What I want is every single comment of this Author that have a RootMessage of the Article type.</p>
  
  <p>These are the mappings.</p>
  
  <p>Message:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                 assembly="FolkeLib"
                 namespace="Folke.Code.Domain"&gt;
  &lt;class name="Message" discriminator-value="0"&gt;
  &lt;id name="Id"&gt;
    &lt;generator class="native"&gt;&lt;/generator&gt;
  &lt;/id&gt;
  &lt;discriminator column="MessageType" type="Int32"/&gt;
  &lt;timestamp column="ModificationDate" name="ModificationDate"/&gt;
  &lt;property name="CreationDate" index="ArticleCreationDate"/&gt;
  &lt;many-to-one name="Author"/&gt;
  &lt;property name="Text" type="StringClob"&gt;
    &lt;column name="Text" sql-type="text"/&gt;
  &lt;/property&gt;
  &lt;property name="Locked"/&gt;
  &lt;many-to-one name="Locker" not-null="false"/&gt;
  &lt;many-to-one name="RootMessage" not-null="false"/&gt;
  &lt;many-to-one name="ParentMessage" not-null="false"/&gt;
  &lt;property name="Score"/&gt;
  &lt;many-to-one name="Site" not-null="true"/&gt;
  </code></pre>
  
  <p>
      </p>
  
  <p>Article:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                 assembly="FolkeLib"
                 namespace="Folke.Code.Domain"&gt;
  &lt;subclass name="Article" extends="Message" discriminator-value="1"&gt;
  &lt;property name="Title"/&gt;
  &lt;set name="TagSet" lazy="true" table="MessageTag"&gt;
    &lt;key column="MessageId"/&gt;
    &lt;many-to-many class="Tag" column="TagId"/&gt;
  &lt;/set&gt;
  &lt;set name="ImageSet" lazy="true" table="MessageImage"&gt;
    &lt;key column="MessageId"/&gt;
    &lt;many-to-many class="Image" column="ImageId"/&gt;
  &lt;/set&gt;
  &lt;many-to-one name="Forum"/&gt;
  &lt;/subclass&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Comment (class is pretty much empty for now):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                 assembly="FolkeLib"
                 namespace="Folke.Code.Domain"&gt;
    &lt;subclass name="Comment" extends="Message" discriminator-value="2"&gt;
    &lt;/subclass&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Any help appreciated!</p>