NHibernate QueryOver 带有子查询和别名
我正在努力将以下(简化的)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());
问题是上面的 Subqueries
和 Where
语句给了我以下内容(无效)where子句:
where shipment.SubscriptionId is null
当我想要的是:
where shipment.SubscriptionId = subscription.Id
所以在构造SQL时不考虑别名及其行级值,而是使用其初始值null
来与Shipment
的 SubscriptionId
。
更新
使用 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));
I'm struggling to convert the following (simplified) HQL to QueryOver:
select subscription
from Subscription as subscription
where not exists (
from Shipment as shipment
where shipment.Subscription = subscription
and (shipment.DeliveryDate = :deliveryDate)
)
I've come this far:
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());
The problem is that the above Subqueries
and Where
statement gives me the following (invalid) where clause:
where shipment.SubscriptionId is null
when what I want is:
where shipment.SubscriptionId = subscription.Id
So the alias and its row-level value isn't taken into account when constructing the SQL, instead its initial value null
is used to compare to the Shipment
's SubscriptionId
.
Update
With dotjoe's provided solution, I was able to write the QueryOver
statement as follows:
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));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
try