如何使用Criteria编写以下SQL:
select b.Name as Batch, b.Capacity as Capecity,
a.tStudent as Admit, (b.Capacity-a.tStudent) as Availabe
from (
SELECT count(Id) as tStudent, BatchId FROM [dbo].[Student] group by BatchId) as a
left join [dbo].[Batch] as b on a.BatchId = b.Id
答案 0 :(得分:1)
要使用NHibernate,要生成如下查询:
SELECT ...
FROM
(
SELECT ...
) AS a
..
我们必须选择:
第一个选项意味着创建一些view
,并将其映射为实体。如果我们不喜欢视图(或者不能创建它),我们可以使用NHibernate映射的强大功能,元素 <subselect>
:
<class name="MyEntity"... >
<subselect>
SELECT ...
FROM ...
</subselect>
...
第二个选项是使用NHibernate API创建本机/原始SQL:
session.CreateSQLQuery("SELECT ")
它没有从映射中获利,但我们仍然可以应用参数,并从转换中获利......