NHibernate QueryOver带有子查询和别名

本文关键字:查询 别名 QueryOver NHibernate | 更新日期: 2023-09-27 18:11:09

我正在努力将以下(简化)HQL转换为QueryOver:

select subscription
from Subscription as subscription
where not exists (
    from Shipment as shipment
    where shipment.Subscription = subscription
    and (shipment.DeliveryDate  = :deliveryDate)
)

我已经走了这么远:

Subscription subscription = null;
Session.QueryOver(() => subscription)
    .Where(Subqueries.NotExists(QueryOver.Of<Shipment>()
        .Where(shipment => shipment.Subscription == subscription)
        .And(shipment=> shipment.DeliveryDate == deliveryDate)
        .Select(shipment => shipment.Id).DetachedCriteria));
    .TransformUsing(new DistinctRootEntityResultTransformer());

问题是上面的SubqueriesWhere语句给了我以下(无效)子句:

where shipment.SubscriptionId is null

当我想要的是:

where shipment.SubscriptionId = subscription.Id

因此,在构造SQL时不考虑别名及其行级值,而是使用别名的初始值nullShipmentSubscriptionId进行比较。

更新使用dotjoe提供的解决方案,我能够像下面这样编写QueryOver语句:
Subscription subscription = null;
Session.QueryOver(() => subscription)
    .WithSubquery.WhereNotExists(QueryOver.Of<Shipment>()
        .Where(shipment => shipment.Subscription.Id == subscription.Id)
        .And(shipment => shipment.DeliveryDate == deliveryDate)
        .Select(shipment => shipment.Id));

NHibernate QueryOver带有子查询和别名

try

.Where(shipment => shipment.Subscription.Id == subscription.Id)