oracle-databasenhibernatecriteriainline-view

Can I use inline views with the criteria API?


Does NHibernate support inline views using criterias? Google doesn't seem to return any relevant results. Here is the query I need to convert preferably using criterias.

SELECT COUNT (incident_count) AS incident_count,
       SUM (total_customers) AS total_customers,
       MAX (longest_etr) AS longest_etr,
       COUNT (DISTINCT crew_count) AS crew_count
  FROM (SELECT   l.incident_id AS incident_count,
                 i.downstream_cust_qty_total AS total_customers,
                 TO_CHAR (MAX (l.etr_datetime),
                          'MM/DD/YYYY HH24:mi:ss'
                         ) AS longest_etr,
                 ca.crew_no AS crew_count
            FROM district d,
                 LOCATION l,
                 ZONE z,
                 incident_device ID,
                 incident i,
                 crew_action ca
           WHERE l.dist_no = d.dist_no
             AND d.zone_id NOT IN (1008, 1010)
             AND ID.location_id = l.location_id
             AND ID.incident_id = i.incident_id
             AND l.location_id = i.location_id
             AND ca.incident_id = i.incident_id
             AND ca.location_id = l.location_id
             AND ID.call_type_cd IN ('ELEC', 'PLAN')
             AND ID.clue_cd NOT IN (248, 258, 975)
             AND l.fac_job_status_cd IN ('A', 'D', 'F', 'G', 'P', 'U', 'W')
             AND z.zone_id = d.zone_id
             AND ca.crew_action_id = l.crew_action_id
             AND l.dist_no = 24
             AND l.primary_loc_flg = 'T'
        GROUP BY l.incident_id, i.downstream_cust_qty_total, ca.crew_no)

I already have everything converted in the where clause. That part was no problem. Which translates into something like.

GetSession().CreateCriteria(typeof (Incident), () => incidentAlias)
    // Projection
    .SetProjection(
        Projections.ProjectionList()
            .Add(LambdaProjection.Count<Incident>(i => incidentAlias.IncidentId).As(() => IncidentCount))
            .Add(LambdaProjection.Sum<Incident>(i => incidentAlias.DownstreamCustQtyTotal).As(() => TotalCustomers))
            .Add(LambdaProjection.Max<Location>(l => locationAlias.EtrDatetime).As(() => LongestEtr))
            .Add(LambdaProjection.CountDistinct<CrewAction>(ca => crewActionAlias.CrewNo).As(() => CrewCount))
            .Add(LambdaProjection.GroupProperty(() => incidentAlias.IncidentId))
            .Add(LambdaProjection.GroupProperty(() => incidentAlias.DownstreamCustQtyTotal))
            .Add(LambdaProjection.GroupProperty(() => crewActionAlias.CrewNo))
    )
    // Aliases
    .CreateAlias(() => incidentAlias.Locations, () => locationAlias)
    .CreateAlias(() => incidentAlias.IncidentDevices, () => incidentDeviceAlias)
    .CreateAlias(() => incidentAlias.District, () => districtAlias)
    .CreateAlias(() => districtAlias.Zone, () => zoneAlias)
    .CreateAlias(() => locationAlias.CrewAction, () => crewActionAlias)
    // Criterias
    .Add(() => locationAlias.PrimaryLocFlg == "T")
    .Add(() => locationAlias.DistNo == districtNumber)
    .Add(() => zoneAlias.ZoneId != 1008)
    .Add(() => zoneAlias.ZoneId != 1010)
    .Add(SqlExpression.In(() => locationAlias.FacJobStatusCd, new[] { "A", "D", "F", "G", "P", "U", "W" }))
    .Add(SqlExpression.In(() => incidentDeviceAlias.CallTypeCd, new [] { "ELEC", "PLAN" }))
    .Add(() => incidentDeviceAlias.ClueCd != "248")
    .Add(() => incidentDeviceAlias.ClueCd != "258")
    .Add(() => incidentDeviceAlias.ClueCd != "975")
    .SetResultTransformer(Transformers.AliasToBean<Dto>())
    .List<Dto>();

Note that I'm using the Lambda criteria extension. Alternatively, I suppose I could create an additional Dto to select all columns with no aggregate functions then use Linq to do the count/sum/max/count distinct.


Solution

  • I just tried it with HQL and it does not work (would be the same with the Criteria API). What does work, however, is the following:

    select 
      (select count(*) from Table1 t1), 
      (select   sum(*) from Table2 t2) 
    from DummyTable dt
    where rownum <= 1
    

    DummyTable is not doing anything other than being there so NHibernate doesn't cry about it, and rownum <= 1 is there to ensure NHibernate doesn't try to return a list of objects. ;-)