我可以使用标准API的内联视图吗?

时间:2010-09-28 13:45:18

标签: oracle nhibernate criteria inline-view

NHibernate是否支持使用标准的内联视图? Google似乎没有返回任何相关结果。这是我需要转换的查询,最好使用标准。

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)

我已经在where子句中转换了所有内容。那部分没问题。这转化为类似的东西。

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>();

请注意,我正在使用Lambda条件扩展。或者,我想我可以创建一个额外的Dto来选择所有没有聚合函数的列,然后使用Linq来计算count / sum / max / count distinct。

1 个答案:

答案 0 :(得分:1)

我刚用HQL尝试过它并不起作用(与Criteria API相同)。但是,工作原理如下:

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

DummyTable除了在那里之外没有做任何事情,所以NHibernate不会为此哭泣,rownum <= 1确保NHibernate不会尝试返回对象列表。 ; - )