Using NHibernate APIs to select a specific dataset with counter

I have the following domain configured to save to NHibernate: Domain

I use PaperConfiguration as the root collection.

I want to highlight all PaperConfiguration objects for a given level and AcademicYearConfiguration. This works very well, as in the following example:

ICriteria criteria =
session.CreateCriteria<PaperConfiguration>()
    .Add(Restrictions.Eq("AcademicYearConfiguration", configuration))
    .CreateCriteria("Paper")
    .CreateCriteria("Unit")
    .CreateCriteria("Tier")
        .Add(Restrictions.Eq("Id", tier.Id))

return criteria.List<PaperConfiguration>();

(There may be a better way to do this, though).

But you also need to know how many ReferenceMaterials are there for each PaperConfiguration, and I would like to get it in the same call. Avoid HQL - I already have an HQL solution for it.

I know that these are forecasts and this question offers an idea, but I cannot get it to work.

PaperConfigurationView, IList<ReferenceMaterial> ReferenceMaterials ReferenceMaterialCount

ICriteria criteria =
session.CreateCriteria<PaperConfiguration>()
    .Add(Restrictions.Eq("AcademicYearConfiguration", configuration))
    .CreateCriteria("Paper")
    .CreateCriteria("Unit")
    .CreateCriteria("Tier")
        .Add(Restrictions.Eq("Id", tier.Id))
    .SetProjection(
        Projections.ProjectionList()
           .Add(Projections.Property("IsSelected"), "IsSelected")
           .Add(Projections.Property("Paper"), "Paper")
            // and so on for all relevant properties
           .Add(Projections.Count("ReferenceMaterials"), "ReferenceMaterialCount")
    .SetResultTransformer(Transformers.AliasToBean<PaperConfigurationView>());

return criteria.List< PaperConfigurationView >();

, . ?

:

ICriteria criteria =
session.CreateCriteria<PaperConfiguration>()
.CreateCriteria("ReferenceMaterials")
.SetProjection(
Projections.ProjectionList()
.Add(Projections.Property("Id"), "Id")
.Add(Projections.Count("ReferenceMaterials"), "ReferenceMaterialCount")
).SetResultTransformer(Transformers.AliasToBean<PaperConfigurationView>());
 return criteria.List< PaperConfigurationView >();

SQL:

SELECT 
  this_.Id as y0_, 
  count(this_.Id) as y1_ 
FROM Domain.PaperConfiguration this_ 
  inner join Domain.ReferenceMaterial referencem1_ 
    on this_.Id=referencem1_.PaperConfigurationId

ADO.NET, SQL, count (referencem1_.Id), (this_.Id).

NHibernate:

  <class name="PaperConfiguration" table="PaperConfiguration">
    <id name="Id" type="Int32">
      <column name="Id" sql-type="int" not-null="true" unique="true" index="PK_PaperConfiguration"/>
      <generator class="native" />
    </id>
    <!-- IPersistent -->
    <version name="VersionLock" />
    <!-- IAuditable -->
    <property name="WhenCreated" type="DateTime" />
    <property name="CreatedBy" type="String" length="50" />
    <property name="WhenChanged" type="DateTime" />
    <property name="ChangedBy" type="String" length="50" />

    <property name="IsEmeEnabled" type="boolean" not-null="true" />

    <property name="IsSelected" type="boolean" not-null="true" />

    <many-to-one name="Paper" column="PaperId" class="Paper" not-null="true" access="field.camelcase"/>

    <many-to-one name="AcademicYearConfiguration" column="AcademicYearConfigurationId" class="AcademicYearConfiguration" not-null="true" access="field.camelcase"/>

    <bag name="ReferenceMaterials" generic="true" cascade="delete" lazy="true" inverse="true">
      <key column="PaperConfigurationId" not-null="true" />
      <one-to-many class="ReferenceMaterial" />
    </bag>
  </class>

  <class name="ReferenceMaterial" table="ReferenceMaterial">
    <id name="Id" type="Int32">
      <column name="Id" sql-type="int" not-null="true" unique="true" index="PK_ReferenceMaterial"/>
      <generator class="native" />
    </id>
    <!-- IPersistent -->
    <version name="VersionLock" />
    <!-- IAuditable -->
    <property name="WhenCreated" type="DateTime" />
    <property name="CreatedBy" type="String" length="50" />
    <property name="WhenChanged" type="DateTime" />
    <property name="ChangedBy" type="String" length="50" />

    <property name="Name" type="String" not-null="true" />
    <property name="ContentFile" type="String" not-null="false" />
    <property name="Position" type="int" not-null="false" />
    <property name="CommentaryName" type="String" not-null="false" />
    <property name="CommentarySubjectTask" type="String" not-null="false" />
    <property name="CommentaryPointScore" type="String" not-null="false" />
    <property name="CommentaryContentFile" type="String" not-null="false" />

    <many-to-one name="PaperConfiguration" column="PaperConfigurationId" class="PaperConfiguration" not-null="true"/>
  </class>
+5
1

Projections.GroupProperty() Projections.Property().

+1

All Articles