Search code examples
c#nhibernateprojectionnhibernate-projections

Make Projection on discriminator-value


I have this NHibernate mapping

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="ParentClass" table="myclass" abstract="true" >
    ...
    <discriminator column="mytype" type="string" />
    ...
    <subclass name="SubClass1" discriminator-value="Type1" />
    <subclass name="SubClass2" discriminator-value="Type2" />
  </class>
</hibernate-mapping>

So how can I get Projection of discriminator-value like any property in my DAO when I filtering ParentClass objects? Code in DAO:

...
  using (ISession session = NHibernateSession)
  {
      ICriteria criteria = session.CreateCriteria<ParentClass>("p")
      ...
      criteria.SetProjection(Projections.ProjectionList()
          .Projections.Property("p.Property1")
          .Projections.???  // something to get discriminator-value from myclass.mytype column
      ...
  }
...

Solution

  • This should be the way "p.class":

    criteria.SetProjection(Projections.ProjectionList()
        .Add(Projections.Property("p.Property1"))
        .Add(Projections.Property("p.class"))
        );
    

    17.1.4.1. Alias and property references

    Description                 Syntax               Example
    ...
    Discriminator of an entity  {[aliasname].class}  DISC as {item.class}
    ....